I tried as you suggested...
```
diff --git a/src/dvr/mkmux.c b/src/dvr/mkmux.c
index 881cc47..b2b5d6f 100644
--- a/src/dvr/mkmux.c
+++ b/src/dvr/mkmux.c
@@ -901,8 +901,6 @@ mk_mux_write_pkt(mk_mux_t *mkm, struct th_pkt *pkt)
   
   if(!mkm->error)
     pkt_ref_dec(pkt);
-  else
-    mk_close_cluster(mkm);
 
   return mkm->error;
 }
@@ -977,6 +975,8 @@ mk_mux_close(mk_mux_t *mkm)
 void
 mk_mux_destroy(mk_mux_t *mkm)
 {
+  if(mkm->cluster)
+    htsbuf_queue_flush(mkm->cluster);
   free(mkm->filename);
   free(mkm->tracks);
   free(mkm->title);
```

It didn't help with these memory leaks (they must be happening during streaming and not at the end). Valgrind log below:
```
==23967== 73,855 (40 direct, 73,815 indirect) bytes in 1 blocks are definitely lost in loss record 316 of 322
==23967==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23967==    by 0x425949: htsbuf_append (htsbuf.c:110)
==23967==    by 0x43C2B9: mk_mux_write_pkt (mkmux.c:759)
==23967==    by 0x444C8C: tvh_muxer_write_pkt (muxer_tvh.c:125)
==23967==    by 0x43D4D1: http_stream_run.isra.1 (webui.c:201)
==23967==    by 0x43D90B: http_stream (webui.c:614)
==23967==    by 0x4086BC: http_exec (http.c:343)
==23967==    by 0x408A82: http_cmd_get (http.c:374)
==23967==    by 0x408C21: process_request (http.c:454)
==23967==    by 0x408FFF: http_serve (http.c:748)
==23967==    by 0x4074A8: tcp_server_start (tcp.c:401)
==23967==    by 0x5C41E99: start_thread (pthread_create.c:308)
==23967== 
==23967== 176,597 (80 direct, 176,517 indirect) bytes in 2 blocks are definitely lost in loss record 318 of 322
==23967==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23967==    by 0x425949: htsbuf_append (htsbuf.c:110)
==23967==    by 0x43A8CF: ebml_append_id (ebml.c:39)
==23967==    by 0x43AA50: ebml_append_bin (ebml.c:71)
==23967==    by 0x43AB67: ebml_append_uint (ebml.c:90)
==23967==    by 0x43C38A: mk_mux_write_pkt (mkmux.c:732)
==23967==    by 0x444C8C: tvh_muxer_write_pkt (muxer_tvh.c:125)
==23967==    by 0x43D4D1: http_stream_run.isra.1 (webui.c:201)
==23967==    by 0x43D90B: http_stream (webui.c:614)
==23967==    by 0x4086BC: http_exec (http.c:343)
==23967==    by 0x408A82: http_cmd_get (http.c:374)
==23967==    by 0x408C21: process_request (http.c:454)
==23967== 
==23967== 206,088 (240 direct, 205,848 indirect) bytes in 6 blocks are definitely lost in loss record 319 of 322
==23967==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23967==    by 0x43C3CF: mk_mux_write_pkt (mkmux.c:652)
==23967==    by 0x444C8C: tvh_muxer_write_pkt (muxer_tvh.c:125)
==23967==    by 0x43D4D1: http_stream_run.isra.1 (webui.c:201)
==23967==    by 0x43D90B: http_stream (webui.c:614)
==23967==    by 0x4086BC: http_exec (http.c:343)
==23967==    by 0x408A82: http_cmd_get (http.c:374)
==23967==    by 0x408C21: process_request (http.c:454)
==23967==    by 0x408FFF: http_serve (http.c:748)
==23967==    by 0x4074A8: tcp_server_start (tcp.c:401)
==23967==    by 0x5C41E99: start_thread (pthread_create.c:308)
==23967==    by 0x5F4ADBC: clone (clone.S:112)
==23967== 
==23967== 211,838 (72 direct, 211,766 indirect) bytes in 3 blocks are definitely lost in loss record 320 of 322
==23967==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23967==    by 0x42581C: htsbuf_queue_alloc (htsbuf.c:57)
==23967==    by 0x43C363: mk_mux_write_pkt (mkmux.c:727)
==23967==    by 0x444C8C: tvh_muxer_write_pkt (muxer_tvh.c:125)
==23967==    by 0x43D4D1: http_stream_run.isra.1 (webui.c:201)
==23967==    by 0x43D90B: http_stream (webui.c:614)
==23967==    by 0x4086BC: http_exec (http.c:343)
==23967==    by 0x408A82: http_cmd_get (http.c:374)
==23967==    by 0x408C21: process_request (http.c:454)
==23967==    by 0x408FFF: http_serve (http.c:748)
==23967==    by 0x4074A8: tcp_server_start (tcp.c:401)
==23967==    by 0x5C41E99: start_thread (pthread_create.c:308)
```