Skip to content

Critical Race Condition in SynthEngine.resetControllers() #12

Description

@hakaru

Description

The newly added resetControllers() method in SynthEngine.swift introduces a critical race condition.

Analysis

  • Unsafe Access: resetControllers() modifies voicesDX7 (via voicesDX7[i].sustained, applyPitchBend) and controller state variables (modWheelDepth, etc.).
  • Concurrency Context: This method is called from loadSlotParams, which is executed on the UI/Main thread. However, voicesDX7 and the controller variables are actively used by the render method on the high-priority Audio thread.
  • Violation: This violates the single-writer principle for the audio thread resources and breaks the lock-free architecture. Accessing UnsafeMutablePointer concurrently without locks results in undefined behavior.

Suggested Fix

Do not modify audio thread state directly from the UI thread.

  1. Use the existing midiRing to send a "Reset All Controllers" message (e.g., CC 121 or a custom event).
  2. Handle the reset logic exclusively within drainMIDI() or the render loop, ensuring thread locality.

Affected File

Sources/M2DXCore/Engine/SynthEngine.swift

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions