Martchus / syncthingtray

Tray application and Dolphin/Plasma integration for Syncthing
https://martchus.github.io/syncthingtray/
Other
1.5k stars 42 forks source link

Release preserved ports on windows, or show correct error on windows #250

Closed eromoe closed 2 months ago

eromoe commented 2 months ago

Today I find icon became grey , tried many way without success. The logs is

[WARNING] Failed starting API: listen tcp 127.0.0.1:8384: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
[WARNING] Starting API/GUI: listen tcp 127.0.0.1:8384: bind: An attempt was made to access a socket in a way forbidden by its access permissions.
[INFO]    Relay listener (dynamic+https://relays.syncthing.net/endpoint) shutting down
[INFO]    QUIC listener ([::]:22000) starting
[INFO]    QUIC listener ([::]:22000) shutting down
[INFO]    TCP listener ([::]:22000) starting
[INFO]    Ready to synchronize datasets_h5 (sendreceive)
[INFO]    Ready to synchronize "Default Folder" (default) (sendreceive)
[INFO]    Ready to synchronize "My Websites" (psmgg-a3dcp) (sendreceive)
[INFO]    Ready to synchronize "github_me" (pyt3c-pyefz) (sendreceive)
[INFO]    Ready to synchronize ".ssh" (qfh39-e4vvj) (sendreceive)
[INFO]    Ready to synchronize ".config/clash" (w4csw-gno2q) (sendreceive)
[INFO]    TCP listener ([::]:22000) shutting down
[INFO]    Exiting

But netstat -aon|findstr "8384" output nothing . After digging, I found that windows may preserve some port (strange behaviour)

netsh interface ipv4 show excludedportrange protocol=tcp :

协议 tcp 端口排除范围

开始端口    结束端口
----------    --------
      1028        1127
      1128        1227
      1228        1327
      1328        1427
      1429        1528
      1529        1628
      1629        1728
      1729        1828
      2869        2869
      5357        5357
      8308        8407
      8408        8507
      9851        9950
     13028       13127
     13128       13227
     14798       14897
     50000       50059     *

* - 管理的端口排除。

The damn preserved behaviour . So the error message is not accurate for debug . And it would be nice to automatically release the preserved ports ( which hyper-v takes ) .

Martchus commented 2 months ago

Note that the log you're showing comes from Syncthing itself (even though it is shown within Syncthing Tray's UI) and it is also Syncthing itself that runs into this error. So it would be best to come up with steps to reproduce the problem using just syncthing.exe and report the problem on https://github.com/syncthing/syncthing/issues/new/choose. It would also make sense to explicitly state the expected behavior which supposedly is that Syncthing tries harder on Windows to release ports that would otherwise end up as "preserved".

However, I'm not sure whether it'll be possible to output a better error message. The error message you've got ("An attempt was made to access a socket in a way forbidden by its access permissions.") is not wrong per-se and probably all that Windows provides as error message for this failing bind-call.

After digging, I found that windows may preserve some port (strange behaviour)

I honestly don't know much about this. When creating another ticket for Syncthing itself it would make sense to add links to the resources you found during your research as other's probably also don't know what you mean. (The only thing that come's to mind is SO_REUSEADDR (see https://man7.org/linux/man-pages/man7/socket.7.html) but I'm not sure whether that's even a thing under Windows. EDIT: Looks like it is: https://learn.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse)


When creating a ticket for Syncthing itself you can drop the link to it here.