Skip to content

Commit 04a58a2

Browse files
authored
Fix WASM onnxruntime pthread abort() crash and SHERPA_ONNX_LOGE output (#3599)G
* Fix WASM onnxruntime pthread abort() crash and SHERPA_ONNX_LOGE output onnxruntime 1.24.4 WASM build spawns background pthreads via the default Ort::Env constructor that call abort(). Fix by using CreateEnvWithGlobalThreadPools with single-threaded pools in a new CreateOrtEnv() helper (ort-env.h). Per-session threading is still configured via session.cc SetIntraOpNumThreads. Also fix SHERPA_ONNX_LOGE for WASM: fprintf(stdout) output is lost due to pthread buffering. Use emscripten_log() which writes directly to the JS console.
1 parent ae2bc66 commit 04a58a2

65 files changed

Lines changed: 232 additions & 130 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ if(SHERPA_ONNX_ENABLE_WASM)
337337
# that pre-spawns a worker pool. Browsers require COOP/COEP headers to
338338
# expose SharedArrayBuffer; wasm/serve.py is provided for that.
339339
string(APPEND CMAKE_C_FLAGS " -pthread")
340-
string(APPEND CMAKE_CXX_FLAGS " -pthread")
340+
string(APPEND CMAKE_CXX_FLAGS " -pthread -fexceptions")
341341
endif()
342342

343343
if(SHERPA_ONNX_ENABLE_WASM_KWS)

sherpa-onnx/csrc/hifigan-vocoder.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) 2024 Xiaomi Corporation
44

55
#include "sherpa-onnx/csrc/hifigan-vocoder.h"
6+
#include "sherpa-onnx/csrc/ort-env.h"
67

78
#include <memory>
89
#include <string>
@@ -30,7 +31,7 @@ class HifiganVocoder::Impl {
3031
public:
3132
explicit Impl(int32_t num_threads, const std::string &provider,
3233
const std::string &model)
33-
: env_(ORT_LOGGING_LEVEL_ERROR),
34+
: env_(CreateOrtEnv()),
3435
sess_opts_(GetSessionOptions(num_threads, provider)),
3536
allocator_{} {
3637
sess_ = std::make_unique<Ort::Session>(
@@ -41,7 +42,7 @@ class HifiganVocoder::Impl {
4142
template <typename Manager>
4243
explicit Impl(Manager *mgr, int32_t num_threads, const std::string &provider,
4344
const std::string &model)
44-
: env_(ORT_LOGGING_LEVEL_ERROR),
45+
: env_(CreateOrtEnv()),
4546
sess_opts_(GetSessionOptions(num_threads, provider)),
4647
allocator_{} {
4748
auto buf = ReadFile(mgr, model);

sherpa-onnx/csrc/macros.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <stdlib.h>
99

1010
#include <utility>
11+
#if SHERPA_ONNX_ENABLE_WASM
12+
#include <emscripten.h>
13+
#endif
1114
#if __OHOS__
1215
#include "hilog/log.h"
1316

@@ -34,12 +37,14 @@
3437
#elif defined(__OHOS__)
3538
#define SHERPA_ONNX_LOGE(...) OH_LOG_INFO(LOG_APP, ##__VA_ARGS__)
3639
#elif SHERPA_ONNX_ENABLE_WASM
37-
#define SHERPA_ONNX_LOGE(...) \
38-
do { \
39-
fprintf(stdout, "%s:%s:%d ", __FILE__, __func__, \
40-
static_cast<int>(__LINE__)); \
41-
fprintf(stdout, ##__VA_ARGS__); \
42-
fprintf(stdout, "\n"); \
40+
#define SHERPA_ONNX_LOGE(...) \
41+
do { \
42+
char _buf[4096]; \
43+
snprintf(_buf, sizeof(_buf), ##__VA_ARGS__); \
44+
emscripten_log(EM_LOG_CONSOLE | EM_LOG_NO_PATHS | EM_LOG_ERROR, \
45+
"%s:%s:%d %s", \
46+
__FILE__, __func__, static_cast<int>(__LINE__), \
47+
_buf); \
4348
} while (0)
4449
#else
4550
#define SHERPA_ONNX_LOGE(...) \

sherpa-onnx/csrc/offline-canary-model.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) 2025 Xiaomi Corporation
44

55
#include "sherpa-onnx/csrc/offline-canary-model.h"
6+
#include "sherpa-onnx/csrc/ort-env.h"
67

78
#include <algorithm>
89
#include <cmath>
@@ -36,7 +37,7 @@ class OfflineCanaryModel::Impl {
3637
public:
3738
explicit Impl(const OfflineModelConfig &config)
3839
: config_(config),
39-
env_(ORT_LOGGING_LEVEL_ERROR),
40+
env_(CreateOrtEnv()),
4041
sess_opts_(GetSessionOptions(config)),
4142
allocator_{} {
4243
encoder_sess_ = std::make_unique<Ort::Session>(
@@ -51,7 +52,7 @@ class OfflineCanaryModel::Impl {
5152
template <typename Manager>
5253
Impl(Manager *mgr, const OfflineModelConfig &config)
5354
: config_(config),
54-
env_(ORT_LOGGING_LEVEL_ERROR),
55+
env_(CreateOrtEnv()),
5556
sess_opts_(GetSessionOptions(config)),
5657
allocator_{} {
5758
{

sherpa-onnx/csrc/offline-ced-model.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) 2024 Xiaomi Corporation
44

55
#include "sherpa-onnx/csrc/offline-ced-model.h"
6+
#include "sherpa-onnx/csrc/ort-env.h"
67
#include "sherpa-onnx/csrc/macros.h"
78

89
#include <memory>
@@ -22,7 +23,7 @@ class OfflineCEDModel::Impl {
2223
public:
2324
explicit Impl(const AudioTaggingModelConfig &config)
2425
: config_(config),
25-
env_(ORT_LOGGING_LEVEL_ERROR),
26+
env_(CreateOrtEnv()),
2627
sess_opts_(GetSessionOptions(config)),
2728
allocator_{} {
2829
sess_ = std::make_unique<Ort::Session>(
@@ -33,7 +34,7 @@ class OfflineCEDModel::Impl {
3334
#if __ANDROID_API__ >= 9
3435
Impl(AAssetManager *mgr, const AudioTaggingModelConfig &config)
3536
: config_(config),
36-
env_(ORT_LOGGING_LEVEL_ERROR),
37+
env_(CreateOrtEnv()),
3738
sess_opts_(GetSessionOptions(config)),
3839
allocator_{} {
3940
auto buf = ReadFile(mgr, config_.ced);

sherpa-onnx/csrc/offline-cohere-transcribe-model.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) 2026 Xiaomi Corporation
44

55
#include "sherpa-onnx/csrc/offline-cohere-transcribe-model.h"
6+
#include "sherpa-onnx/csrc/ort-env.h"
67

78
#include <algorithm>
89
#include <cmath>
@@ -43,7 +44,7 @@ class OfflineCohereTranscribeModel::Impl {
4344
public:
4445
explicit Impl(const OfflineModelConfig &config)
4546
: config_(config),
46-
env_(ORT_LOGGING_LEVEL_ERROR),
47+
env_(CreateOrtEnv()),
4748
sess_opts_(GetSessionOptions(config)),
4849
allocator_{},
4950
cpu_mem_info_(
@@ -67,7 +68,7 @@ class OfflineCohereTranscribeModel::Impl {
6768
template <typename Manager>
6869
Impl(Manager *mgr, const OfflineModelConfig &config)
6970
: config_(config),
70-
env_(ORT_LOGGING_LEVEL_ERROR),
71+
env_(CreateOrtEnv()),
7172
sess_opts_(GetSessionOptions(config)),
7273
allocator_{},
7374
cpu_mem_info_(

sherpa-onnx/csrc/offline-ct-transformer-model.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) 2024 Xiaomi Corporation
44

55
#include "sherpa-onnx/csrc/offline-ct-transformer-model.h"
6+
#include "sherpa-onnx/csrc/ort-env.h"
67
#include "sherpa-onnx/csrc/macros.h"
78

89
#include <memory>
@@ -30,7 +31,7 @@ class OfflineCtTransformerModel::Impl {
3031
public:
3132
explicit Impl(const OfflinePunctuationModelConfig &config)
3233
: config_(config),
33-
env_(ORT_LOGGING_LEVEL_ERROR),
34+
env_(CreateOrtEnv()),
3435
sess_opts_(GetSessionOptions(config)),
3536
allocator_{} {
3637
sess_ = std::make_unique<Ort::Session>(
@@ -41,7 +42,7 @@ class OfflineCtTransformerModel::Impl {
4142
template <typename Manager>
4243
Impl(Manager *mgr, const OfflinePunctuationModelConfig &config)
4344
: config_(config),
44-
env_(ORT_LOGGING_LEVEL_ERROR),
45+
env_(CreateOrtEnv()),
4546
sess_opts_(GetSessionOptions(config)),
4647
allocator_{} {
4748
auto buf = ReadFile(mgr, config_.ct_transformer);

sherpa-onnx/csrc/offline-ctc-model.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) 2022-2023 Xiaomi Corporation
44

55
#include "sherpa-onnx/csrc/offline-ctc-model.h"
6+
#include "sherpa-onnx/csrc/ort-env.h"
67

78
#include <algorithm>
89
#include <memory>
@@ -51,7 +52,7 @@ enum class ModelType : std::uint8_t {
5152
namespace sherpa_onnx {
5253

5354
static ModelType GetModelType(const std::string &model_path, bool debug) {
54-
Ort::Env env(ORT_LOGGING_LEVEL_ERROR);
55+
Ort::Env env = CreateOrtEnv();
5556
Ort::SessionOptions sess_opts;
5657
sess_opts.SetIntraOpNumThreads(1);
5758
sess_opts.SetInterOpNumThreads(1);
@@ -115,7 +116,7 @@ static ModelType GetModelType(const std::string &model_path, bool debug) {
115116

116117
static ModelType GetModelType(char *model_data, size_t model_data_length,
117118
bool debug) {
118-
Ort::Env env(ORT_LOGGING_LEVEL_ERROR);
119+
Ort::Env env = CreateOrtEnv();
119120
Ort::SessionOptions sess_opts;
120121
sess_opts.SetIntraOpNumThreads(1);
121122
sess_opts.SetInterOpNumThreads(1);

sherpa-onnx/csrc/offline-dolphin-model.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) 2025 Xiaomi Corporation
44

55
#include "sherpa-onnx/csrc/offline-dolphin-model.h"
6+
#include "sherpa-onnx/csrc/ort-env.h"
67

78
#include <algorithm>
89
#include <memory>
@@ -32,7 +33,7 @@ class OfflineDolphinModel::Impl {
3233
public:
3334
explicit Impl(const OfflineModelConfig &config)
3435
: config_(config),
35-
env_(ORT_LOGGING_LEVEL_ERROR),
36+
env_(CreateOrtEnv()),
3637
sess_opts_(GetSessionOptions(config)),
3738
allocator_{} {
3839
sess_ = std::make_unique<Ort::Session>(
@@ -43,7 +44,7 @@ class OfflineDolphinModel::Impl {
4344
template <typename Manager>
4445
Impl(Manager *mgr, const OfflineModelConfig &config)
4546
: config_(config),
46-
env_(ORT_LOGGING_LEVEL_ERROR),
47+
env_(CreateOrtEnv()),
4748
sess_opts_(GetSessionOptions(config)),
4849
allocator_{} {
4950
auto buf = ReadFile(mgr, config_.dolphin.model);

sherpa-onnx/csrc/offline-fire-red-asr-ctc-model.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Copyright (c) 2026 Xiaomi Corporation
44

55
#include "sherpa-onnx/csrc/offline-fire-red-asr-ctc-model.h"
6+
#include "sherpa-onnx/csrc/ort-env.h"
67

78
#include <algorithm>
89
#include <memory>
@@ -32,7 +33,7 @@ class OfflineFireRedAsrCtcModel::Impl {
3233
public:
3334
explicit Impl(const OfflineModelConfig &config)
3435
: config_(config),
35-
env_(ORT_LOGGING_LEVEL_ERROR),
36+
env_(CreateOrtEnv()),
3637
sess_opts_(GetSessionOptions(config)),
3738
allocator_{} {
3839
sess_ = std::make_unique<Ort::Session>(
@@ -44,7 +45,7 @@ class OfflineFireRedAsrCtcModel::Impl {
4445
template <typename Manager>
4546
Impl(Manager *mgr, const OfflineModelConfig &config)
4647
: config_(config),
47-
env_(ORT_LOGGING_LEVEL_ERROR),
48+
env_(CreateOrtEnv()),
4849
sess_opts_(GetSessionOptions(config)),
4950
allocator_{} {
5051
auto buf = ReadFile(mgr, config_.fire_red_asr_ctc.model);

0 commit comments

Comments
 (0)