Commit 99d589a
committed
fix(transcode): per-invocation context — stops goroutine leak
Production goroutine dump on r4dio after 3 days uptime:
1052 select (mirrorTranscodeMetadata)
↑ leaked
total 1073, RSS 777 MB (baseline: ~45 goroutines, 44 MB)
performTranscode spawned mirrorTranscodeMetadata with the outer
runTranscoder context. That ctx is the transcoder's lifetime
context — only cancelled when the operator stops the transcoder.
So every source disconnect / reconnect cycle (~650/day on this
host based on robodj's reconnect pattern) ran another retry of
performTranscode, which spawned another mirror goroutine whose
ctx was never cancelled. The OLD mirror kept ticking forever.
After three days × ~600 reconnects × ~9 transcoders = ~16k
leaked goroutines worth — production showed 1052 actually
accumulated (the rest had presumably been killed by source
shutdowns earlier).
Each leaked goroutine kept references to its `input`/`output`
*Stream and a `*time.Ticker`. ~1 KB live state × 1052 plus
runtime stack overhead = the >700 MB we observed (Go stacks
default to 8 KB; many will have grown larger over their
lifetime).
Fix: derive a per-invocation child context in performTranscode,
defer-cancel it. Pass it to mirrorTranscodeMetadata, EncodeMP3,
EncodeOpus, and DecoderHub.Acquire so all spawned work tied to
this performTranscode call dies when performTranscode returns.
The outer runTranscoder ctx still controls the whole transcoder
lifetime; the new runCtx is just one retry-loop iteration.1 parent f5ce521 commit 99d589a
1 file changed
Lines changed: 18 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
143 | 157 | | |
144 | 158 | | |
145 | 159 | | |
146 | 160 | | |
147 | 161 | | |
148 | 162 | | |
149 | | - | |
| 163 | + | |
150 | 164 | | |
151 | 165 | | |
152 | 166 | | |
| |||
192 | 206 | | |
193 | 207 | | |
194 | 208 | | |
195 | | - | |
| 209 | + | |
196 | 210 | | |
197 | 211 | | |
198 | 212 | | |
| |||
216 | 230 | | |
217 | 231 | | |
218 | 232 | | |
219 | | - | |
| 233 | + | |
220 | 234 | | |
221 | 235 | | |
222 | 236 | | |
| |||
233 | 247 | | |
234 | 248 | | |
235 | 249 | | |
236 | | - | |
| 250 | + | |
237 | 251 | | |
238 | 252 | | |
239 | 253 | | |
| |||
0 commit comments