Occasional streamHandler map memory leak
@fuweid is already working on this.
Since Jun 10, 2026.
Assessment
This issue has not been assessed yet.
Description
Summary
I'm chasing a slow unbounded memory growth in the containerd-shim-runc-v2. After profiling and reviewing the code my conclusion is that there is an occasional memory leak in the ttrpc server where keys in the streams sync.Map are sometimes removed before the corresponding stream id key gets added (in a separate goroutine) causing the key to remain forever until connection is closed.
Background
I'm running containerd on an older ARMv7 32-bit IoT device, we are currently running older 1.7 containerd branch, but the ttrpc library is fairly recent at v1.2.7. Our 2 core CPU is a bit on the slower side so that could perhaps explain why this leak has not been noticed before. In our setup we use containerd directly without any kubernetes/kubelet.
Symptoms
Over a 7 days period the containerd-shim-runc-v2 PSS_Dirty (also confirmed with Golang's HeapAlloc) increases from 4.2 MB to 4.6 MB.
Golangs heap pprof initially didn't initially show any leaks, but after reducing the runtime.MemProfileRate = 1 an 8 hour diff between two heap profiles showed following:
i.e. this streams.Store line https://github.com/containerd/ttrpc/blob/v1.2.7/server.go#L489
Analysis
After adding some logs to the streams.Store and streams.Delete I could see that when the Delete method is called it sometimes tries to remove a key that doesn't exist, when this happened then usually the deletion was also logged before the insertion.
Problems was also a bit easier to reproduce when adding an extra sleep before storing the streamHandler:
diff --git a/vendor/github.com/containerd/ttrpc/server.go b/vendor/github.com/containerd/ttrpc/server.go
index bb71de677..35cfa1723 100644
--- a/vendor/github.com/containerd/ttrpc/server.go
+++ b/vendor/github.com/containerd/ttrpc/server.go
@@ -486,6 +486,7 @@ func (c *serverConn) run(sctx context.Context) {
continue
}
+ time.Sleep(1 * time.Millisecond)
streams.Store(id, sh)
atomic.AddInt32(&active, 1)
}
My takeaway is that there is a race where two different goroutines tries to modify the streams map with the same key. If the service handler runs to completion (line 481) before the streams.Store is called the key will remain forever.
Possible solution
For the containerd use case I've only seen this race on non-streaming messages. So a potential fix is to simply skip storing the streamHandler when it is nil:
diff --git a/vendor/github.com/containerd/ttrpc/server.go b/vendor/github.com/containerd/ttrpc/server.go
index bb71de677..9b9c0bdc7 100644
--- a/vendor/github.com/containerd/ttrpc/server.go
+++ b/vendor/github.com/containerd/ttrpc/server.go
@@ -486,7 +486,9 @@ func (c *serverConn) run(sctx context.Context) {
continue
}
- streams.Store(id, sh)
+ if sh != nil {
+ streams.Store(id, sh)
+ }
atomic.AddInt32(&active, 1)
}
// TODO: else we must ignore this for future compat. log this?
- Dominant language
- Go
- Stars
- 674
- Forks
- 90
- Avg merge
- 16h 32m
- Merged PRs (30d)
- 2
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from containerd/ttrpc
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
containerd/ttrpc#258 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
containerd/ttrpc#212 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
containerd/ttrpc#203 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
containerd/ttrpc#184 · 9 comments ·
-
ttrpc client send hang, even ctx is timeout.Possibly taken @novahe claimed this 251 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 45/100
containerd/ttrpc#174 · 7 comments ·
All issues in containerd/ttrpc
Similar issues
-
P3 tech debt
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
Maintainers usually reply within 1 day
-
kind/feature needs-triage priority/unprioritized
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
NVIDIA/k8s-test-infra#1504 ·
Maintainers usually reply within 1 day
-
Area/Infrastructure Aspect/Infra Resources Type/Bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
wso2/agent-manager#2717 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
open-policy-agent/opa#9685 · 1 comment ·
Maintainers usually reply within 1 day