Skip to content

fix: cancel ALSA receive subscription on disconnect for clean shutdown#13

Open
thorinside wants to merge 1 commit intoInvisibleWrench:masterfrom
thorinside:fix/alsa-clean-shutdown
Open

fix: cancel ALSA receive subscription on disconnect for clean shutdown#13
thorinside wants to merge 1 commit intoInvisibleWrench:masterfrom
thorinside:fix/alsa-clean-shutdown

Conversation

@thorinside
Copy link
Copy Markdown

Problem

On Linux, disconnecting a MIDI device (or shutting down the app) can hang indefinitely. The root cause is that the StreamSubscription from _device.receivedMessages.listen() is never cancelled before the ALSA device is closed, leaving an active read loop blocking the isolate.

Additionally, _setupStreamController and _rxStreamController are not closed during teardown, and iterating _connectedDevices while modifying it during teardown causes a ConcurrentModificationError.

Fix

  • Store the StreamSubscription returned by receivedMessages.listen()
  • Cancel the subscription in disconnect() before closing the ALSA device
  • Close both _setupStreamController and _rxStreamController during teardown
  • Use .toList() when iterating _connectedDevices during teardown to avoid concurrent modification

Related

The underlying dart_midi package also has an ALSA shutdown hang that is fixed separately: maks/dart_midi#8. That PR closes the ALSA port before killing the receive isolate, which complements this fix at the flutter_midi_command_linux level.

Testing

Verified on Linux with:

  • Disconnect during active MIDI streaming — returns immediately, no hang
  • App shutdown with connected device — exits cleanly
  • Repeated connect/disconnect cycles — no crashes or resource leaks

Developed and tested as part of nt_helper.

…utdown

- Store StreamSubscription from _device.receivedMessages.listen()
- Cancel subscription in disconnect() before closing ALSA device
- Close both _setupStreamController and _rxStreamController in teardown()
- Use .toList() to avoid concurrent modification during teardown

Fixes app hang on Linux shutdown caused by uncancelled ALSA receive loop
@maks
Copy link
Copy Markdown
Contributor

maks commented Mar 31, 2026

Thanks for this fix @thorinside 👍🏻
FYI @mortenboye I have now merged @thorinside PR with the fix on the dart_midi package side and published it as 0.1.1

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.

2 participants