Skip to content

[BUG] SIGABRT for multiple inference requests concurrently on GPT-oss (vector::back on empty) #337

Closed

Description

@ronaldmannak

Summary

I’m seeing a deterministic crash during GPT-OSS inference when multiple requests run concurrently. The crash disappears when I force concurrency to 1. Other models (like Qwen) can run multiple requests without any issues. The issue seems specific to GPT-OSS.

The abort comes from MLX’s trace/compile path and asserts on vector::back() being called on an empty vector.

Environment

Steps to Reproduce

  1. Run GPT-OSS inference with concurrent requests (e.g., multiple client requests at once).
  2. Observe crash during generation/compile phase.
  3. Set concurrency to 1 (serialize requests) and rerun.
  4. Crash disappears.

Expected Behavior

Concurrent inference requests should be safe (or fail gracefully), without crashing the process.

Actual Behavior

Process aborts with SIGABRT. Console shows:

Stack Trace (excerpt)

Task 153 Queue : com.apple.root.user-initiated-qos.cooperative (concurrent)
#0  __pthread_kill
ml-explore/mlx-swift-lm#1  pthread_kill
ml-explore/mlx-swift-lm#2  abort
ml-explore/mlx-swift-lm#3  std::__1::__libcpp_verbose_abort
ml-explore/mlx-swift-lm#4  std::__1::vector<...>::back at __vector/vector.h:430
ml-explore/mlx-swift-lm#5  mlx::core::detail::InTracing::~InTracing at mlx/transforms_impl.h:28
ml-explore/mlx-swift-lm#6  mlx::core::detail::InTracing::~InTracing at mlx/transforms_impl.h:27
ml-explore/mlx-swift-lm#7  mlx::core::detail::compile_trace at mlx/compile.cpp:410
ml-explore/mlx-swift-lm#8  mlx::core::detail::compile(...) at mlx/compile.cpp:1125
ml-explore/mlx-swift-lm#16 mlx::core::detail::compile(...) at mlx/compile.cpp:1179
ml-explore/mlx-swift-lm#24 ::mlx_closure_apply at mlx/c/closure.cpp:102
ml-explore/mlx-swift-lm#25 CompiledFunction.innerCall at Transforms+Compile.swift:100
ml-explore/mlx-swift-lm#29 CompiledFunction.call at Transforms+Compile.swift:40
ml-explore/mlx-swift-lm#31 SwiGLUSwitchGLU.callAsFunction at GPTOSS.swift:144
ml-explore/mlx-swift-lm#35 GPTOSSModel.callAsFunction at GPTOSS.swift:509
ml-explore/mlx-swift-lm#37 LanguageModel.callAsFunction at LanguageModel.swift:183
ml-explore/mlx-swift-lm#39 TokenIterator.step at Evaluate.swift:421
ml-explore/mlx-swift-lm#41 LLMRunner.generateHarmonyTokenStreaming at LLMRunner+HarmonyGeneration.swift:68
ml-explore/mlx-swift-lm#40	TokenIterator.next() at Evaluate.swift:445
ml-explore/mlx-swift-lm#41	in closure ml-explore/mlx-swift-lm#1 in closure ml-explore/mlx-swift-lm#1 in closure ml-explore/mlx-swift-lm#2 in LLMRunner.generateHarmonyTokenStreaming(tokenIds:modelId:languageModelContainer:generateParameters:maxCompletionTokens:stopTokens:) ()
ml-explore/mlx-swift-lm#42	in partial apply for closure ml-explore/mlx-swift-lm#1 in closure ml-explore/mlx-swift-lm#1 in closure ml-explore/mlx-swift-lm#2 in LLMRunner.generateHarmonyTokenStreaming(tokenIds:modelId:languageModelContainer:generateParameters:maxCompletionTokens:stopTokens:) ()
ml-explore/mlx-swift-lm#43	ModelContainer.perform<τ_0_0>(_:) at ModelContainer.swift:71

Notes / Hypothesis

  • The crash happens only when multiple requests are processed concurrently.
  • Setting concurrency to 1 makes the crash disappear.
  • This only happens on GPT-OSS, not other models

Activity

  1. davidkoski commented on Jan 16, 2026

    @davidkoski
    Member

    Something under compile(). Likely this is not thread safe, but typically we don't hit it:

            var compiled = mlx_closure_new()
            mlx_detail_compile(&compiled, innerClosure, id, shapeless, [], 0)
            defer { mlx_closure_free(compiled) }

    Moving over to mlx-swift as it is in the core. I will come up with a repro and get the proper locks in place.

  2. davidkoski commented on Jan 16, 2026

    @davidkoski
    Member

    This may be a two-parter. First, the calls to compile should not be able to crash it. Second, I think this (GPTOSS):

    private func compileSwiglu() -> @Sendable (MLXArray, MLXArray) -> MLXArray {
        compile(shapeless: true) { xLinear, xGlu in
            swiglu(xLinear, xGlu)
        }
    }

    could be done more like this (Activations.swift):

    private let compiledRelu: @Sendable (MLXArray) -> MLXArray = {
        compile(shapeless: true) { x in
            maximum(x, 0)
        }
    }()

    That would avoid the issue, but not fix the underlying problem. I think both should probably be done.

  3. ronaldmannak commented on Jan 16, 2026

    @ronaldmannak
    ContributorAuthor

    The Python implementation already caches SwiGLU as a compiled property. In PR #51 I mirrored that in GPTOSS.swift by switching from compileSwiglu() to a cached compiledSwiglu, to keep the Swift implementation in sync with Python:

    https://github.com/ml-explore/mlx-swift-lm/pull/51/files#diff-a79fb464ca4ed3e086d1f44b73e2051f8485bba0845322b90ec6dcd33f98cf85L94-L98

    However, it somehow still crashed. Which is odd, unless, unless... I had the wrong local branch of mlx-swift-lm open when testing... a dumb mistake I would obviously never make... Right?... You know, I'll be right back...

  4. added a commit that references this issue on Jan 16, 2026
    bfb302a
  5. ronaldmannak commented on Jan 16, 2026

    @ronaldmannak
    ContributorAuthor

    @davidkoski Confirmed: I was accidentally running the tests on main. After switching to the #51 branch (which uses the cached compiledSwiglu property), the crash goes away.

    It still seems worth making compile thread-safe in general, but in this case it looks like the issue will be resolved once #51 is merged. Should I close this issue?

  6. ronaldmannak commented on Jan 16, 2026

    @ronaldmannak
    ContributorAuthor

    @davidkoski Just saw you created #339. Together with #51 this issue is solved. I'll close the issue

  7. ronaldmannak commented on Jan 16, 2026

    @ronaldmannak
    ContributorAuthor

    BTW, I was referring to the mlx-swift-lm PR 51 ( ml-explore/mlx-swift-lm#51 ) where I originally opened the issue

  8. davidkoski commented on Jan 16, 2026

    @davidkoski
    Member

    Ah perfect! Yeah, I think 51 is the way to go as it makes sure we only compile it once. I will review that now.

    We could still hit crashes if two different functions we compiled at the same time and #339 will deal with that.

  9. added a commit that references this issue on Jan 22, 2026
    b058eda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions