shadowsocks-rust icon indicating copy to clipboard operation
shadowsocks-rust copied to clipboard

Sockets are not properly closed on Windows when used as a library

Open faern opened this issue 5 years ago • 7 comments

If using shadowsocks-rust as a library. And one want to keep the process running after terminating the shadowsocks part. Then the shadowsocks sockets are left open and are never properly closed on Windows.

This is likely due to this bug: https://github.com/carllerche/mio/issues/776

I'm not sure if that bug can be solved in itself, thus making mio better. Or if there is anything simple one can do here in shadowsocks in order to not trigger the problem. But I want to leave this issue here so it's documented for now at least.

faern avatar Jan 29 '19 12:01 faern

It seems possible to work around this issue. When hitting this bug we ran Shadowsocks in the following fashion:

let result = runtime.block_on(shadowsocks_future.select2(abort_signal));
match result {...}
runtime.shutdown_now().wait().unwrap();

But as described above this left the listening sockets Shadowsocks had open forever, so the ports could not be reused.

However, this version seems to work fine:

let result = runtime.block_on(shadowsocks_future.select2(abort_signal));
match result {...}
mem::drop(result);
runtime.shutdown_now().wait().unwrap();

The thing is that select2 returns the future that did not complete. As such shadowsocks_future lives inside of result and is still existing when the runtime shuts down. If I make sure to properly drop the shadowsocks future before shutting down the runtime, then I can't reproduce the problem.

faern avatar Mar 01 '19 10:03 faern

Or you can wrap it in a block:

{
    let result = runtime.block_on(shadowsocks_future.select2(abort_signal));
    match result {...}
}
runtime.shutdown_now().wait().unwrap();

zonyitoo avatar Mar 01 '19 13:03 zonyitoo

Yes, there are many ways to get rid of result. The main point being that what causes the problem is if the runtime is shut down while some listening socket futures are still in scope.

faern avatar Mar 01 '19 18:03 faern

I have provided a feature trust-dns to optionally enable dependency trust-dns-resolver.

zonyitoo avatar Feb 09 '20 16:02 zonyitoo

tokio 0.3 has been released and it includes mio 0.7. Updating tokio (and mio) might help fix issue.

oherrala avatar Oct 16 '20 09:10 oherrala

ref #301

zonyitoo avatar Oct 16 '20 09:10 zonyitoo

Will the libraries be updated? https://github.com/tokio-rs/tokio/releases/tag/tokio-1.19.2 https://github.com/bluejekyll/trust-dns/releases/tag/v0.21.2

wendig0x avatar Jun 18 '22 11:06 wendig0x