Skip to content

STUN should periodically perform re-discovery #8341

Open

Description

@omgold

I run Syncthing 1.20.1 on Linux.

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.

Activity

  1. added
    bug
    needs-triage
    on May 13, 2022
  2. bt90 commented on May 13, 2022

    @bt90
    Contributor

    This sounds like a duplicate of #8243

  3. omgold commented on May 13, 2022

    @omgold
    Author

    This sounds like a duplicate of #8243

    @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
    
  4. AudriusButkevicius commented on May 13, 2022

    @AudriusButkevicius
    Member

    Thanks, its now clear what the issue is, but we usually restart after detecting sleep to mitigate stuff like this, is that not working?

  5. omgold commented on May 13, 2022

    @omgold
    Author

    Doesn't look like. What exactly is supposed to be restarted automatically? The whole process?

  6. AudriusButkevicius commented on May 13, 2022

    @AudriusButkevicius
    Member

    Yes, thats the default.

  7. omgold commented on May 14, 2022

    @omgold
    Author

    Okay, seems I found the issue, then:

    The code to detect the restart is this

    			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:

    		l.Infof("standby monitor: before: %s, after: %s, diff: %s, wakeup: %s, opt: %s", now, time.Now(), time.Since(now), time.Since(now) > 2*time.Minute, cfg.Options().RestartOnWakeup)
    

    I get:

    [H4352] INFO: standby monitor: before: 2022-05-14 07:55:30.73093779 +0200 CEST m=+140.036064024, after: 2022-05-14 07:58:54.876967931 +0200 CEST m=+152.102880692, diff: 12.066816719s, wakeup: %!s(bool=false), opt: %!s(bool=true)
    

    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.

  8. AudriusButkevicius commented on May 14, 2022

    @AudriusButkevicius
    Member

    Wtf? How is that 12 seconds when the wallclock time is 2 mins?

  9. omgold commented on May 14, 2022

    @omgold
    Author

    @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.

  10. AudriusButkevicius commented on May 14, 2022

    @AudriusButkevicius
    Member

    I get that, I just find it very surprising.

  11. omgold commented on May 14, 2022

    @omgold
    Author

    Yeah, quite a bit counterintuitive.

    The fix is simple, though:

    --- src/github.com/syncthing/syncthing/cmd/syncthing/main.go.orig	2022-05-14 15:13:31.192225539 +0200
    +++ src/github.com/syncthing/syncthing/cmd/syncthing/main.go	2022-05-14 15:13:52.694070436 +0200
    @@ -763,7 +763,7 @@
     
     func standbyMonitor(app *syncthing.App, cfg config.Wrapper) {
     	restartDelay := 60 * time.Second
    -	now := time.Now()
    +	now := time.Now().Round(0)
     	for {
     		time.Sleep(10 * time.Second)
     		if time.Since(now) > 2*time.Minute && cfg.Options().RestartOnWakeup {
    @@ -777,7 +777,7 @@
     			app.Stop(svcutil.ExitRestart)
     			return
     		}
    -		now = time.Now()
    +		now = time.Now().Round(0)
     	}
     }
    
  12. AudriusButkevicius commented on May 14, 2022

    @AudriusButkevicius
    Member

    Feel free to open a PR given you already got a diff.

  13. removed
    needs-triage
    on May 14, 2022
  14. changed the title [-]STUN re-discovery not happening after suspend/sleep of host[/-] [+]STUN should periodically perform re-discovery[/+] on May 14, 2022
  15. added theissue type on May 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions