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
Suspecting a memory leak related to QUIC connections #8243
bugA problem with current functionality, as opposed to missing functionality (enhancement)frozen-due-to-ageIssues closed and untouched for a long time, together with being locked for discussionneeds-triageNew issues needed to be validated
If your Syncthing log reports panics because of database corruption it is
most likely a fault with your system's storage or memory. Affected log
entries will contain lines starting with panic: leveldb. You will need to
delete the index database to clear this, by running syncthing -reset-database.
No
Include required information
Please be sure to include at least:
which version of Syncthing and what operating system you are using
After running for a few days syncthing takes up almost 1 GB of RAM, which is unexpected for a machine with just 1 shared folder with: 269 files, 22 folders and ~628 MiB total size. The contents of the folder don't change that often, and a few days ago the RAM usage was way lower. syncthing hasn't been interacted with in these last two days. syncthing is running for about a week now.
I know that this bug report is not super helpful. I am not a go developer, and have no idea how to debug. It is also a little tricky, because memory usage seems to ramp over time. Could be that due to the 32GB RAM, garbage collection doesn't kick in yet?
It's a buffer pool for packets. So might be valid usage due to a lot of churn (seems overly excessive though), or quic-go failing to hand buffers back to the pool in some places. Can you get such profiles at different times to see if it continuously increases (which would align with the overall memory usage you see, but that might also have other causes).
I've had this issue as well. After a while, the memory usage grows 10x. Also, there's a constant 20-25% CPU usage. After restarting, it goes down to ~0.6%. Nothing is being synced and I don't see anything unusual in the logs.
The linked quic-go issue talks about not closing connections if I understand correctly. In our code it should be guaranteed that close is called, unless something is hung (which should then show in the traces from above).
The problem is all these operations are blocking, there's no way to pass a context/deadline to them. So even if the hang was detected, the goroutines would stick around.
Anyway a relevant problem in the current context is quic.Connection.CloseWithError blocking: It waits for sends to finish before returning, thus we never close the actual connection here. Maybe having a deadline there to unblock in quic would be appropriate (@marten-seemann ?). And we definitely can do that when closing the quic conn on our side: Wait 10s for the session to close, and if it doesn't go ahead closing everything else cutting our losses (leaving that on goroutine pending, but not accumulating further ones like it's happening here). Ugly, but less ugly than status quo.
Does WriteMsgUDP not respect write deadlines? It seems one should be set, to be able to fail with a timeout at some point. (Maybe internally in the quic package, maybe from our side if it's exposed.)
We had a similar issue way back with TLS/TCP connections because Close() does a write, and if we're closing because the connection is dead that will block forever. Setting a write deadline before close works.
That's actually it: Due to all the wrapping in quic conns, the deadline we set on the tls conn before closing is not applied to the underlying udp conn. And it's not as simple as just setting that deadline, as all quic connections from the listener share the underlying udp connection - we don't want to set a (very short) deadline for all quic connections when closing one.
One option would be to set a long timeout on the listener udp-conn (something in line with the ping timeout in the protocol). However there's no possibility to set a timeout, just deadlines - so we'd have to wrap the udp-conn to override all write methods to extend the deadline on every successful write. Very ugly.
Yeah... I'm not aware of any reasonable circumstances where an UDP write should block for an appreciable amount of time, really, so to me this feels like something that should happen in the QUIC package prior to each write. If the UDP write is still blocked after a minute or something, it feels safe to say that something bad has happened and that connection is dead...
I remember vaguely that it was discussed at some point to abandon the current wrapping/pfilter approach and reimplement it as a handler for non-QUIC packets passed to quic-go.
Looking at the quic server code it would be doable to add a write timeout via net.PacketConn.SetWriteDeadline in the sendQueue, possibly configurable through the *Config parameter. @marten-seemann Does that approach seem sensible and if yes, would you welcome such a contribution?
I remember vaguely that it was discussed at some point to abandon the current wrapping/pfilter approach and reimplement it as a handler for non-QUIC packets passed to quic-go.
I don't remember these or know what it's abouth. However my "wrapping comment" wasn't correct, it plays a role butit isn't really the fundamental issue here. That's the difference between tcp and quic, where in the former we have one tcp conn per "syncthing connection", while for the latter it's a single udp conn with multiplexed "syncthing/quic connections" on top.
Looking at the quic server code it would be doable to add a write timeout via net.PacketConn.SetWriteDeadline in the sendQueue, possibly configurable through the *Config parameter. @marten-seemann Does that approach seem sensible and if yes, would you welcome such a contribution?
Not sure I understand how that's supposed to work. It's still the same underlying net.PacketConn, so the deadline would apply to all connections. I'd be very worried about the performance impact of setting a write deadline for every packet sent.
I'd like to raise the question again why we're not raising this problem with the Go team. This is a Go bug after all.
Not sure I understand how that's supposed to work. It's still the same underlying net.PacketConn, so the deadline would apply to all connections.
The deadline would indeed apply to all connections from one quic server/Listener with it's underlying connection. That seems like a desirable property, as all those connections are affected if the underlying connection is broken/timing out.
I'd be very worried about the performance impact of setting a write deadline for every packet sent.
That can be done more smartly than setting it on eery write, e.g. by setting a deadline and only extending it some time before it expires.
I'd like to raise the question again why we're not raising this problem with the Go team. This is a Go bug after all.
I don't understand this: Why is a blocking/hanging udp conn clearly a go bug (as in not an issue with the underlying system outside of go's control)?
Probably not. quic-go/quic-go#3409 would cause a connection to be dropped, but shouldn't result in a memory leak (unless your code doesn't properly deal with closed connections).
I'm going to consider this either solved, or need of refreshed troubleshooting if not, as there have been a lot of water under the bridge (new quic-go releases, new Go releases).
bugA problem with current functionality, as opposed to missing functionality (enhancement)frozen-due-to-ageIssues closed and untouched for a long time, together with being locked for discussionneeds-triageNew issues needed to be validated
Activity
leipert commentedon Mar 30, 2022
I know that this bug report is not super helpful. I am not a go developer, and have no idea how to debug. It is also a little tricky, because memory usage seems to ramp over time. Could be that due to the 32GB RAM, garbage collection doesn't kick in yet?
How can I help debugging this?
calmh commentedon Mar 30, 2022
I run Syncthing on M1. Currently with just over a day's uptime, but it's at about 56 MiB resident, syncing 154 GiB of data.
AudriusButkevicius commentedon Mar 30, 2022
Feels like this should live on the forum?
calmh commentedon Mar 30, 2022
Let's see what the profile says
leipert commentedon Mar 30, 2022
@calmh Here are the profiles:
syncthing-profiles.zip
Sorry if this isn't the right place to report this, @AudriusButkevicius
leipert commentedon Mar 30, 2022
Had a look with pprof. Seems like a lot of memory is related to
quic. Wonder if quic-go/quic-go#3322 is somehow related 🤔AudriusButkevicius commentedon Mar 30, 2022
I don't think we churn through streams, we have one per connection. I'm on the phone so can't check the profile to give any more meaningful insights.
imsodin commentedon Mar 30, 2022
It's a buffer pool for packets. So might be valid usage due to a lot of churn (seems overly excessive though), or quic-go failing to hand buffers back to the pool in some places. Can you get such profiles at different times to see if it continuously increases (which would align with the overall memory usage you see, but that might also have other causes).
charleshan commentedon Mar 30, 2022
I've had this issue as well. After a while, the memory usage grows 10x. Also, there's a constant 20-25% CPU usage. After restarting, it goes down to ~0.6%. Nothing is being synced and I don't see anything unusual in the logs.
leipert commentedon Mar 30, 2022
@imsodin Just created a profile, about 4h later, saw an increase of 100MB since then:
syncthing-heap-darwin-arm64-v1.19.1-003026.pprof.zip
imsodin commentedon Mar 31, 2022
Accompanied by a 30MB increase in that particalur pool in the profile.
Can you als grap goroutine traces please, to check if there's an unexpected amount of open streams or something - would be surprising but better be sure http://localhost:8384/debug/pprof/goroutine
The linked quic-go issue talks about not closing connections if I understand correctly. In our code it should be guaranteed that close is called, unless something is hung (which should then show in the traces from above).
leipert commentedon Mar 31, 2022
@imsodin That URL doesn't work. I assume I need to restart with
STPROFILERset, as per: https://docs.syncthing.net/branch/untrusted/html/dev/debugging.htmltomasz1986 commentedon Mar 31, 2022
The URL is http://localhost:9090/debug/pprof/goroutine actually 😉.
STPROFILERdoes need to be enable beforehand though.16 remaining items
imsodin commentedon Apr 11, 2022
The problem is all these operations are blocking, there's no way to pass a context/deadline to them. So even if the hang was detected, the goroutines would stick around.
Anyway a relevant problem in the current context is
quic.Connection.CloseWithErrorblocking: It waits for sends to finish before returning, thus we never close the actual connection here. Maybe having a deadline there to unblock in quic would be appropriate (@marten-seemann ?). And we definitely can do that when closing the quic conn on our side: Wait 10s for the session to close, and if it doesn't go ahead closing everything else cutting our losses (leaving that on goroutine pending, but not accumulating further ones like it's happening here). Ugly, but less ugly than status quo.calmh commentedon Apr 12, 2022
Does WriteMsgUDP not respect write deadlines? It seems one should be set, to be able to fail with a timeout at some point. (Maybe internally in the quic package, maybe from our side if it's exposed.)
We had a similar issue way back with TLS/TCP connections because Close() does a write, and if we're closing because the connection is dead that will block forever. Setting a write deadline before close works.
imsodin commentedon Apr 12, 2022
That's actually it: Due to all the wrapping in quic conns, the deadline we set on the tls conn before closing is not applied to the underlying udp conn. And it's not as simple as just setting that deadline, as all quic connections from the listener share the underlying udp connection - we don't want to set a (very short) deadline for all quic connections when closing one.
One option would be to set a long timeout on the listener udp-conn (something in line with the ping timeout in the protocol). However there's no possibility to set a timeout, just deadlines - so we'd have to wrap the udp-conn to override all write methods to extend the deadline on every successful write. Very ugly.
calmh commentedon Apr 13, 2022
Yeah... I'm not aware of any reasonable circumstances where an UDP write should block for an appreciable amount of time, really, so to me this feels like something that should happen in the QUIC package prior to each write. If the UDP write is still blocked after a minute or something, it feels safe to say that something bad has happened and that connection is dead...
bt90 commentedon Apr 16, 2022
I remember vaguely that it was discussed at some point to abandon the current wrapping/pfilter approach and reimplement it as a handler for non-QUIC packets passed to quic-go.
imsodin commentedon Apr 16, 2022
Looking at the quic server code it would be doable to add a write timeout via
net.PacketConn.SetWriteDeadlinein thesendQueue, possibly configurable through the*Configparameter. @marten-seemann Does that approach seem sensible and if yes, would you welcome such a contribution?I don't remember these or know what it's abouth. However my "wrapping comment" wasn't correct, it plays a role butit isn't really the fundamental issue here. That's the difference between tcp and quic, where in the former we have one tcp conn per "syncthing connection", while for the latter it's a single udp conn with multiplexed "syncthing/quic connections" on top.
marten-seemann commentedon Apr 16, 2022
Not sure I understand how that's supposed to work. It's still the same underlying
net.PacketConn, so the deadline would apply to all connections. I'd be very worried about the performance impact of setting a write deadline for every packet sent.I'd like to raise the question again why we're not raising this problem with the Go team. This is a Go bug after all.
imsodin commentedon Apr 16, 2022
The deadline would indeed apply to all connections from one quic server/
Listenerwith it's underlying connection. That seems like a desirable property, as all those connections are affected if the underlying connection is broken/timing out.That can be done more smartly than setting it on eery write, e.g. by setting a deadline and only extending it some time before it expires.
I don't understand this: Why is a blocking/hanging udp conn clearly a go bug (as in not an issue with the underlying system outside of go's control)?
bt90 commentedon May 23, 2022
@marten-seemann is this issue related to our problem here?
quic-go/quic-go#3409
marten-seemann commentedon May 23, 2022
Probably not. quic-go/quic-go#3409 would cause a connection to be dropped, but shouldn't result in a memory leak (unless your code doesn't properly deal with closed connections).
calmh commentedon Mar 10, 2023
I'm going to consider this either solved, or need of refreshed troubleshooting if not, as there have been a lot of water under the bridge (new quic-go releases, new Go releases).
leipert commentedon Mar 11, 2023
Makes sense to me, haven't noticed the problem lately!