From bcf1ed01631fd5811bd7f1c2eb82ae6397dbbb87 Mon Sep 17 00:00:00 2001 From: Sacha Arbonel Date: Mon, 5 May 2025 07:16:54 +0200 Subject: [PATCH] server: update abort mechanism to handle HTTP connection closure (#3112) --- examples/server/server.cpp | 40 +++++++++++++++----------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/examples/server/server.cpp b/examples/server/server.cpp index 1e56d45c..97c12bdb 100644 --- a/examples/server/server.cpp +++ b/examples/server/server.cpp @@ -843,33 +843,25 @@ int main(int argc, char ** argv) { wparams.progress_callback_user_data = &user_data; } - // examples for abort mechanism - // in examples below, we do not abort the processing, but we could if the flag is set to true - - // the callback is called before every encoder run - if it returns false, the processing is aborted - { - static bool is_aborted = false; // NOTE: this should be atomic to avoid data race - - wparams.encoder_begin_callback = [](struct whisper_context * /*ctx*/, struct whisper_state * /*state*/, void * user_data) { - bool is_aborted = *(bool*)user_data; - return !is_aborted; - }; - wparams.encoder_begin_callback_user_data = &is_aborted; - } - - // the callback is called before every computation - if it returns true, the computation is aborted - { - static bool is_aborted = false; // NOTE: this should be atomic to avoid data race - - wparams.abort_callback = [](void * user_data) { - bool is_aborted = *(bool*)user_data; - return is_aborted; - }; - wparams.abort_callback_user_data = &is_aborted; - } + // tell whisper to abort if the HTTP connection closed + wparams.abort_callback = [](void *user_data) { + // user_data is a pointer to our Request + auto req_ptr = static_cast(user_data); + return req_ptr->is_connection_closed(); + }; + wparams.abort_callback_user_data = (void*)&req; if (whisper_full_parallel(ctx, wparams, pcmf32.data(), pcmf32.size(), params.n_processors) != 0) { + // handle failure or early abort + if (req.is_connection_closed()) { + // log client disconnect + fprintf(stderr, "client disconnected, aborted processing\n"); + res.status = 499; // Client Closed Request (nginx convention) + res.set_content("{\"error\":\"client disconnected\"}", "application/json"); + return; + } fprintf(stderr, "%s: failed to process audio\n", argv[0]); + res.status = 500; // Internal Server Error const std::string error_resp = "{\"error\":\"failed to process audio\"}"; res.set_content(error_resp, "application/json"); return;