Control API closed for good when a config write races a reload that recreates the API
Which version are you using?
v1.21.0 (also reproduced on v1.19.1; the code is unchanged on main as of 2026-09-27)
Which operating system are you using?
Linux amd64 standard
Describe the issue
The Control API can shut down permanently. A config write that arrives while
the core is reloading, where that reload closes and recreates the API server,
deadlocks with it. The log ends at [API] closing and nothing is logged after
that:
- the API port is never reopened (
connection refusedfrom then on); - no server affected by the reload is restarted (
createResources()never runs); - the process keeps running, so a supervisor such as systemd sees nothing wrong.
Only restarting MediaMTX recovers it. In production it left one of our nodes
without its API for 2.5 days. Two processes pushed configuration through the API
at the same time right after MediaMTX started: one sent a global patch
(runOnConnect), the other sent path writes.
This is related to #6077, which was fixed by #6101, but the cycle is different:
Core.run()receives write #1 (a global patch that requires the API to be recreated, e.g. arunOnConnectchange). It replies, then callsreloadConf()on the same goroutine.closeResources()→API.Close()→httpp.Server.Close()runsShutdown()with its 2 s timeout, thenhandlerTracker.close(), which waits for every running handler with no timeout.- Write #2 entered its handler just before the close. It is blocked in
Core.APIConfigPathsPatch(), trying to send onchAPIConfigPathPatch. OnlyCore.run()reads that channel, and it is stuck in step 2.p.ctxis not cancelled, so the handler'sselectnever returns.
Goroutine dump (kill -QUIT) of the hung process:
goroutine 39 [sync.WaitGroup.Wait]:
sync.(*WaitGroup).Wait
httpp.(*handlerTracker).close handler_tracker.go:20
httpp.(*Server).Close server.go:219
core.(*Core).closeResources core.go:1253
core.(*Core).reloadConf core.go:1343
core.(*Core).run core.go:418
goroutine 19 [select]: (6 goroutines like this)
core.(*Core).APIConfigPathsPatch core.go:1489
api.(*API).onConfigPathsPatch api_config_paths.go:92
httpp.(*handlerTracker).ServeHTTP handler_tracker.go:35A client cannot reliably avoid this. The write handler answers 200 before the
reload, so the client has no way to know when it is safe to send the next write
(#6077 makes the same point).
A fix is proposed in #6279
Describe how to replicate the issue
Describe how to replicate the issue
Start MediaMTX with the configuration below. Every server except the API is off; they are not needed for this.
Send one global patch that makes the API reload, and several path writes at the same time:
A=http://127.0.0.1:9997/v3 curl -s -m 10 -X PATCH -H 'Content-Type: application/json' \ -d '{"runOnConnect":"true 1"}' $A/config/global/patch & for k in 1 2 3 4 5 6 7 8; do curl -s -m 10 -X PATCH -H 'Content-Type: application/json' \ -d '{"sourceOnDemand":false}' $A/config/paths/patch/test & done waitWait a few seconds, then query the API:
sleep 3; curl -s -m 3 $A/config/paths/listThe connection is refused. It stays refused until MediaMTX is restarted.
It hangs on the first attempt every time we tried, on both versions. To confirm
the stacks, send kill -QUIT <pid> to the hung process.
MediaMTX configuration
logLevel: debug
api: yes
apiAddress: 127.0.0.1:9997
rtsp: no
rtmp: no
hls: no
webrtc: no
srt: no
moq: no
playback: no
metrics: no
pprof: no
paths:
test:MediaMTX logs
INF MediaMTX v1.21.0
INF [API] started with listener on 127.0.0.1:9997 (TCP/HTTP)
INF reloading configuration (API request)
INF reloading configuration (API request)
INF reloading configuration (API request)
INF [API] closingNothing is logged after [API] closing: no [API] started, no error.
Packet dump
No response
Source: bluenviron/mediamtx