From 2a91524dbf17aec123baf78e86a2ebbe4f49ad2a Mon Sep 17 00:00:00 2001 From: HieDean <34408026+HieDean@users.noreply.github.com> Date: Fri, 24 Nov 2023 10:23:25 +0800 Subject: [PATCH] Lock before push_back the deque for thread safety (#445) Co-authored-by: hiedean --- sherpa-onnx/csrc/online-websocket-server-impl.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sherpa-onnx/csrc/online-websocket-server-impl.cc b/sherpa-onnx/csrc/online-websocket-server-impl.cc index eaf65cfa9..a47e613c8 100644 --- a/sherpa-onnx/csrc/online-websocket-server-impl.cc +++ b/sherpa-onnx/csrc/online-websocket-server-impl.cc @@ -304,7 +304,10 @@ void OnlineWebsocketServer::OnMessage(connection_hdl hdl, int32_t num_samples = payload.size() / sizeof(float); std::vector samples(p, p + num_samples); - c->samples.push_back(std::move(samples)); + { + std::lock_guard lock(c->mutex); + c->samples.push_back(std::move(samples)); + } asio::post(io_work_, [this, c]() { decoder_.AcceptWaveform(c); }); break;