Skip to content

Conversation

@markterm
Copy link
Contributor

@markterm markterm commented Jan 2, 2026

When pressing 'apply' to reload a script, the code had a thread-safety issue: multiple threads were executing Lua operations on the same lua_State concurrently without proper synchronization.

The race condition occurred because:

  1. UI thread would load/compile new script WITHOUT holding the lock
  2. Audio thread could be running script->process() at the same time
  3. Both threads manipulate the same Lua stack -> corruption
  4. This caused DSPScriptPosition userdata to become invalid
  5. Accessing position methods (bar(), beat(), etc.) would crash with null pointer dereference

The fix moves the ScopedLock acquisition to the very beginning of loadScript(), ensuring ALL Lua operations are serialized:

  • Script loading and compilation
  • DSPScript construction
  • Script preparation
  • Pointer swap
  • Old script cleanup (release, cleanup, destructor)

This prevents any concurrent Lua state access. The tradeoff is a brief audio dropout during script reload, which is acceptable for this operation.

Fixes crash: Access violation reading location 0x0000000000000000 in DSPScriptPosition::_bar() and DSPScriptPosition::_beat()

🤖 Generated with Claude Code

When pressing 'apply' to reload a script, the code had a critical
thread-safety issue: multiple threads were executing Lua operations
on the same lua_State concurrently without proper synchronization.

The race condition occurred because:
1. UI thread would load/compile new script WITHOUT holding the lock
2. Audio thread could be running script->process() at the same time
3. Both threads manipulate the same Lua stack -> corruption
4. This caused DSPScriptPosition userdata to become invalid
5. Accessing position methods (bar(), beat(), etc.) would crash
   with null pointer dereference

The fix moves the ScopedLock acquisition to the very beginning of
loadScript(), ensuring ALL Lua operations are serialized:
- Script loading and compilation
- DSPScript construction
- Script preparation
- Pointer swap
- Old script cleanup (release, cleanup, destructor)

This prevents any concurrent Lua state access. The tradeoff is a
brief audio dropout during script reload, which is acceptable for
this operation.

Fixes crash: Access violation reading location 0x0000000000000000
in DSPScriptPosition::_bar() and DSPScriptPosition::_beat()

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@CLAassistant
Copy link

CLAassistant commented Jan 2, 2026

CLA assistant check
All committers have signed the CLA.

@mfisher31
Copy link
Member

This looks reasonable thanks! But I can't merge until the CLA is signed: #1031 (comment)

@markterm
Copy link
Contributor Author

markterm commented Jan 2, 2026

Signed (FYI the host this occured in was Maschine)

@mfisher31
Copy link
Member

Awesome. thanks!

@mfisher31 mfisher31 merged commit 244c004 into kushview:main Jan 3, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants