#6280·mediamtx

Control API closed for good when a config write races a reload that recreates the API

Author: ftaiolivistaCreated Sep 28, 2026Updated Sep 28, 2026

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 refused from 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:

  1. Core.run() receives write #1 (a global patch that requires the API to be recreated, e.g. a runOnConnect change). It replies, then calls reloadConf() on the same goroutine.
  2. closeResources() → API.Close() → httpp.Server.Close() runs Shutdown() with its 2 s timeout, then handlerTracker.close(), which waits for every running handler with no timeout.
  3. Write #2 entered its handler just before the close. It is blocked in Core.APIConfigPathsPatch(), trying to send on chAPIConfigPathPatch. Only Core.run() reads that channel, and it is stuck in step 2. p.ctx is not cancelled, so the handler's select never 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:35

A 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

  1. Start MediaMTX with the configuration below. Every server except the API is off; they are not needed for this.

  2. Send one global patch that makes the API reload, and several path writes at the same time:

    bash
    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
    wait
  3. Wait a few seconds, then query the API:

    bash
    sleep 3; curl -s -m 3 $A/config/paths/list

    The 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

yaml
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] closing

Nothing is logged after [API] closing: no [API] started, no error.

Packet dump

No response