You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
STUN should periodically perform re-discovery #8341
I notice, when I use quic with NAT traversal via STUN, that after putting the host into suspend mode (S3) for a prolonged time, no new quic connections are established. When I look for the entry on the discosrv, the host inserts again the previous remote/NAT'ed IP/port which it had before the suspend. More often than not these are not correct anymore, of course.
Looking around a bit in the code, I understand the logic here, such that no STUN servers are contacted again, as long as there is traffic on the local port. As the peer still has a quic address advertised, there will be (only outgoing) traffic to try to reestablish a connection. Thus the discovery address is never updated and the firewall of the peer remains closed, as it tries to contact only the old address.
@bt90 Can you explain? That ticket seems to be about a memory leak and hanging udp sockets, no?
My issue is presumedly about STUN.
Effectively it looks like this. After startup STUN is contacted:
Mai 13 16:38:48 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Starting stun for Stun@udp://[::]:22002
Mai 13 16:38:48 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Running stun for Stun@udp://[::]:22002 via stun.syncthing.net:3478
Mai 13 16:39:07 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Stun@udp://[::]:22002 detected NAT type: Port restricted NAT via stun.syncthing.net:3478
Mai 13 16:39:07 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Stun@udp://[::]:22002 starting stun keepalive via stun.syncthing.net:3478, next sleep 2m0s
Mai 13 16:39:07 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Stun@udp://[::]:22002 stun sleeping for 1m58.393040513s
At this point the quic connection is established, as it should. And STUN keepalive is skipped, which is okay for now:
Mai 13 16:41:06 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Stun@udp://[::]:22002 stun last write gap less than next sleep: 3.001268997s < 2m0s. Will try later
Mai 13 16:41:06 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Stun@udp://[::]:22002 stun sleeping for 1m56.998671102s
Now I put the host to sleep and wait a couple of minutes. After coming up again the port mapping has changed. The quic connection has long been dropped by the peer and times out on this host, too. Both sides try to establish a new connection, but the peer does not know the changed mapping on the local side.
This doesn't change, as the STUN module still considers that to be unnecessary, presumedly due to the outgoing traffic:
Mai 13 16:49:11 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Stun@udp://[::]:22002 stun last write gap less than next sleep: 7.027295716s < 2m0s. Will try later
Mai 13 16:49:11 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Stun@udp://[::]:22002 stun sleeping for 1m52.972663592s
Mai 13 16:51:04 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Stun@udp://[::]:22002 stun last write gap less than next sleep: 2.001374795s < 2m0s. Will try later
Mai 13 16:51:04 laptop.localdomain syncthing[959189]: [H4352] DEBUG: Stun@udp://[::]:22002 stun sleeping for 1m57.998586559s
if time.Since(now) > 2*time.Minute && cfg.Options().RestartOnWakeup {
l.Infof("Paused state detected, possibly woke up from standby. Restarting in %v.", restartDelay)
But the manual for the go time packages says this:
Operating systems provide both a “wall clock,” which is subject to changes for clock synchronization, and a “monotonic clock,” which is not. The general rule is that the wall clock is for telling time and the monotonic clock is for measuring time. Rather than split the API, in this package the Time returned by time.Now contains both a wall clock reading and a monotonic clock reading; later time-telling operations use the wall clock reading, but later time-measuring operations, specifically comparisons and subtractions, use the monotonic clock reading.
And indeed, when I print out the values like this:
In short, time.Since() is not the right thing to measure differences in walltime.
But back to the STUN issue. In my experience with NAT traversal, it is far from guaranteed that mapping do not change, even with the per-15s keepalive pings the quic implementation does. So IMHO, occasionally doing STUN re-discovery even when a connection is alive would be a good thing to have.
@AudriusButkevicius The quote from the manual explains it. time.Since() does not calculate the difference in wallclock time, but monotonic time. And monotonic time is apparently halted during system suspend.
changed the title [-]STUN re-discovery not happening after suspend/sleep of host[/-][+]STUN should periodically perform re-discovery[/+]on May 14, 2022
Activity
bt90 commentedon May 13, 2022
This sounds like a duplicate of #8243
omgold commentedon May 13, 2022
@bt90 Can you explain? That ticket seems to be about a memory leak and hanging udp sockets, no?
My issue is presumedly about STUN.
Effectively it looks like this. After startup STUN is contacted:
At this point the quic connection is established, as it should. And STUN keepalive is skipped, which is okay for now:
Now I put the host to sleep and wait a couple of minutes. After coming up again the port mapping has changed. The quic connection has long been dropped by the peer and times out on this host, too. Both sides try to establish a new connection, but the peer does not know the changed mapping on the local side.
This doesn't change, as the STUN module still considers that to be unnecessary, presumedly due to the outgoing traffic:
AudriusButkevicius commentedon May 13, 2022
Thanks, its now clear what the issue is, but we usually restart after detecting sleep to mitigate stuff like this, is that not working?
omgold commentedon May 13, 2022
Doesn't look like. What exactly is supposed to be restarted automatically? The whole process?
AudriusButkevicius commentedon May 13, 2022
Yes, thats the default.
omgold commentedon May 14, 2022
Okay, seems I found the issue, then:
The code to detect the restart is this
But the manual for the go time packages says this:
Operating systems provide both a “wall clock,” which is subject to changes for clock synchronization, and a “monotonic clock,” which is not. The general rule is that the wall clock is for telling time and the monotonic clock is for measuring time. Rather than split the API, in this package the Time returned by time.Now contains both a wall clock reading and a monotonic clock reading; later time-telling operations use the wall clock reading, but later time-measuring operations, specifically comparisons and subtractions, use the monotonic clock reading.
And indeed, when I print out the values like this:
I get:
In short,
time.Since()is not the right thing to measure differences in walltime.But back to the STUN issue. In my experience with NAT traversal, it is far from guaranteed that mapping do not change, even with the per-15s keepalive pings the quic implementation does. So IMHO, occasionally doing STUN re-discovery even when a connection is alive would be a good thing to have.
AudriusButkevicius commentedon May 14, 2022
Wtf? How is that 12 seconds when the wallclock time is 2 mins?
omgold commentedon May 14, 2022
@AudriusButkevicius The quote from the manual explains it.
time.Since()does not calculate the difference in wallclock time, but monotonic time. And monotonic time is apparently halted during system suspend.AudriusButkevicius commentedon May 14, 2022
I get that, I just find it very surprising.
omgold commentedon May 14, 2022
Yeah, quite a bit counterintuitive.
The fix is simple, though:
AudriusButkevicius commentedon May 14, 2022
Feel free to open a PR given you already got a diff.
[-]STUN re-discovery not happening after suspend/sleep of host[/-][+]STUN should periodically perform re-discovery[/+]