Repository navigation
Question about / problem with the API change of removing is_resp from the connection struct #3788
|
I upgraded to Mongoose 7.24 and I stumbled across the change that In my project I use many code snippets like this: mg_printf(nc, "HTTP/1.1 200 OK\r\n"
"%s"
"Content-Length: %lu\r\n"
"Connection: %s\r\n"
"\r\n",
headers,
(unsigned long)len,
(nc->data[2] == 'C' ? "close" : "keep-alive")
);
mg_send(nc, data, len);
nc->is_resp = 0;Is it correct to simply replace After I changed it, I get following dump from libasan: Is this expected and I should better use mg_http_reply instead of mg_printf and mg_send or is it another problem? Thanks for mongoose and your support! |
Replies: 2 comments 5 replies
|
Yes, The crash comes from doing it after In myMPD that path is The smallest fix I found is to only clear it when it still holds the "response started" marker: void webserver_handle_connection_close(struct mg_connection *nc) {
if (nc->data[2] == 'C') {
MYMPD_LOG_DEBUG(NULL, "Set connection %lu to is_draining", nc->id);
nc->is_draining = 1;
}
if (nc->pfn_data == nc) {
nc->pfn_data = NULL; // response done; leave static file serving alone
}
}
I checked this against 7.24 with a small ASan build: a handler that calls Switching to |
|
Hello Jürgen, sadly that was omitted in the release notes, just added it:
71e0176 has it done and we internally do that https://mongoose.ws/documentation/#best-practices Of course crashes should not happen unless you're sending some (other) NULL pointer (not c->pfn_data), so if you can isolate that I'll be happy to investigate. |

Yes,
nc->pfn_data = NULLis the 7.24 replacement foris_resp = 0when you wrote the response yourself withmg_printf/mg_send.mg_http_replydoes the same thing at the end (http.c#L471).http_cbsetspfn_data = cbeforeMG_EV_HTTP_MSGand won't parse the next pipelined request until it's NULL again (L1150-L1152, L1245-L1246).The crash comes from doing it after
mg_http_serve_file/mg_http_serve_dir. In 7.24pfn_datais no longer just a flag: while a file is streaming, it holds the openstruct mg_fd *andpfnisstatic_cb(L672-L673).static_cbreads the file fromc->pfn_dataon the next poll (L489-L497), so setting it to NULL there gives you a NULLfdand that SEGV. In 7.23is_respwas a se…