shadowsocks-rust
shadowsocks-rust copied to clipboard
Sockets are not properly closed on Windows when used as a library
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.
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.
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();
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.
I have provided a feature trust-dns
to optionally enable dependency trust-dns-resolver
.
tokio 0.3 has been released and it includes mio 0.7. Updating tokio (and mio) might help fix issue.
ref #301
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