fix: client lost the last segment of audio in online mode#3301
Conversation
Fixed the issue where the client lost the last segment of audio in online mode and ensured that the server no longer returns offline results in online mode. In online mode, you can see that in the figure, 2pass-online misses the character “型”, and the result can only be obtained using 2pass-offline. Also, offline results should not be returned in online mode.
There was a problem hiding this comment.
Code Review
This pull request updates the handle_result function in websocket-server-2pass.cpp to accept a modetype parameter, allowing the server to skip offline results when in 'online' mode to prevent overwriting online results. The review feedback suggests two improvements: first, perform a null check on the return value of FunASRGetTpassResult before constructing a std::string to avoid potential undefined behavior and segmentation faults; second, pass the modetype parameter by const std::string& instead of by value to prevent unnecessary copy operations.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| std::string tmp_tpass_msg = FunASRGetTpassResult(result, 0); | ||
| if (tmp_tpass_msg != "") { |
There was a problem hiding this comment.
The C-style API function FunASRGetTpassResult returns a const char* which could potentially be nullptr. Constructing a std::string directly from a nullptr results in undefined behavior and can cause a segmentation fault. It is safer to perform a null check before constructing the std::string.
| std::string tmp_tpass_msg = FunASRGetTpassResult(result, 0); | |
| if (tmp_tpass_msg != "") { | |
| const char* tpass_res = FunASRGetTpassResult(result, 0); | |
| std::string tmp_tpass_msg = tpass_res ? tpass_res : ""; | |
| if (!tmp_tpass_msg.empty()) { |
| } | ||
|
|
||
| nlohmann::json handle_result(FUNASR_RESULT result, websocketpp::connection_hdl& hdl, std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,std::owner_less<websocketpp::connection_hdl>>& data_map) { | ||
| nlohmann::json handle_result(FUNASR_RESULT result, websocketpp::connection_hdl& hdl, std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,std::owner_less<websocketpp::connection_hdl>>& data_map, std::string modetype) { |
There was a problem hiding this comment.
Passing std::string by value can result in unnecessary copy operations. It is more efficient to pass it by const std::string& to avoid copying the string on every call.
| nlohmann::json handle_result(FUNASR_RESULT result, websocketpp::connection_hdl& hdl, std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,std::owner_less<websocketpp::connection_hdl>>& data_map, std::string modetype) { | |
| nlohmann::json handle_result(FUNASR_RESULT result, websocketpp::connection_hdl& hdl, std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,std::owner_less<websocketpp::connection_hdl>>& data_map, const std::string& modetype) { |
Summary
Fixed the issue where the client lost the last segment of audio in online mode and ensured that the server no longer returns offline results in online mode. In online mode, you can see that in the figure, 2pass-online misses the character “型”, and the result can only be obtained using 2pass-offline. Also, offline results should not be returned in online mode.
Type of change
Validation
before


after
python -m compileall funasr examples testsUser impact
New users and production deployers
Notes for reviewers
None