Skip to content

Prune stale connection records when the MIDI mode changes - #6

Merged
sastraxi merged 1 commit into
fix/midi-aggregated-hotplugfrom
fix/stale-connections-on-midi-mode-switch
Aug 21, 2026
Merged

sastraxi merged 1 commit into
fix/midi-aggregated-hotplugfrom
fix/stale-connections-on-midi-mode-switch

Conversation

@sastraxi

Copy link
Copy Markdown
Collaborator

Observed problem

The user does these steps:

  1. Set the MIDI mode to separated. Enable a USB-MIDI device.
  2. Make a MIDI cable from the device to a plugin. The cable operates correctly.
  3. Change the MIDI mode to aggregated.
  4. Change the MIDI mode back to separated. Enable the device again.
  5. Make the same MIDI cable again.

At step 5 the cable does not appear. The web server sends the reply true, but JACK does not make the connection. The user cannot make that cable again in the same session. A restart of mod-ui is necessary.

Cause

self.connections is the record that mod-ui keeps of the JACK graph. The connect() method starts with this test:

if (port_from, port_to) in self.connections:
    print("NOTE: Requested connection already exists")
    callback(True)
    return

The test is correct only if the record agrees with JACK.

In set_midi_devices_change_mode, the code gave the combined "in;out" device symbol to remove_port_from_connections:

for port_symbol, port_alias, port_conns in self.midiports:
    self.remove_port_from_connections(port_symbol)   # "system:midi_capture_6;system:midi_playback_4"

remove_port_from_connections compares the given name with single JACK port names. A combined symbol never agrees with a single name. Thus mod-ui removed no entry from the record.

But mod-host removes these JACK connections. It does this in steps 1 and 2 of effects_aggregated_midi_enable. After step 3 above, the record held a connection that JACK did not have. The test in connect() then finds that entry and stops. mod-ui sends true to the browser, but mod-ui does not send the command to mod-host.

Change

set_midi_devices_change_mode divides the combined symbol before it calls remove_port_from_connections. It gives each half to that method. set_midi_devices_separated already does this with the same data.

Test results

The steps above were done on a pi-Stomp with a Roland EV-1-WL USB-MIDI device. The table shows the JACK connections of system:midi_capture_6.

Step Before the change After the change
2. Cable made mod-host:midi_in, effect_8:events mod-host:midi_in, effect_8:events
3. Aggregated mode mod-midi-merger:in mod-midi-merger:in
4. Separated mode mod-host:midi_in mod-host:midi_in
5. Cable made again mod-host:midi_in — no change mod-host:midi_in, effect_8:events

Scope

This change corrects the record only. It does not change the behavior that MOD intends. mod-host removes these JACK connections when the mode changes to aggregated. This change makes the record of mod-ui agree with that result. mod-ui does not make the cables again when the mode changes back to separated. This behavior does not change.

Upstream

This problem is also in upstream mod-audio/mod-ui at master (commit 0467c103), in the same method. No upstream issue reports this problem. This change can go to upstream.

set_midi_devices_change_mode gave the combined "in;out" device symbol to
remove_port_from_connections, which matches against single JACK port names.
A combined symbol never matches, so nothing was removed from self.connections
and nothing was disconnected in JACK.

mod-host tears those JACK edges down itself in effects_aggregated_midi_enable
(steps 1 and 2), so after a switch to aggregated mode self.connections held
edges that no longer existed. connect() then short-circuits on its
"already exists" guard: the user drags a cable, gets HTTP true, and no
connection is ever made. Switching back to separated mode does not clear it,
so the cable can never be created again in that session.

Split the symbol first, as set_midi_devices_separated already does for the
same data.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

@rreichenbach rreichenbach left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good.

@sastraxi
sastraxi merged commit e06260a into main Aug 21, 2026
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