Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locking #3

Open
wants to merge 3 commits into
base: python-rtmidi
Choose a base branch
from
Open

Locking #3

wants to merge 3 commits into from

Conversation

folkertvanheusden
Copy link

The alsa-thread and main-thread can both access the queue concurrently. This may cause problems. This can be verified using helgrind:

==68196== Possible data race during write of size 4 at 0x4CA44B4 by thread #2
==68196== Locks held: none
==68196==    at 0x5851349: MidiInApi::MidiQueue::push(MidiInApi::MidiMessage const&) (RtMidi.cpp:696)
==68196==    by 0x5856333: alsaMidiHandler(void*) (RtMidi.cpp:1668)
==68196==    by 0x484D82A: ??? (in /usr/libexec/valgrind/vgpreload_helgrind-amd64-linux.so)
==68196==    by 0x4A2D44F: start_thread (pthread_create.c:473)
==68196==    by 0x4B5DD52: clone (clone.S:95)
==68196== 
==68196== This conflicts with a previous read of size 4 by thread #1
==68196== Locks held: none
==68196==    at 0x5851214: MidiInApi::MidiQueue::size(unsigned int*, unsigned int*) (RtMidi.cpp:671)
==68196==    by 0x585145F: MidiInApi::MidiQueue::pop(std::vector<unsigned char, std::allocator<unsigned char> >*, double*, RtMidiError::Type*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*) (RtMidi.cpp:709)
==68196==    by 0x58516B5: MidiInApi::getMessage(std::vector<unsigned char, std::allocator<unsigned char> >*) (RtMidi.cpp:657)
==68196==    by 0x584E54F: getMessage (RtMidi.h:623)
==68196==    by 0x584E54F: __pyx_pf_6rtmidi_7_rtmidi_6MidiIn_12get_message (_rtmidi.cpp:8167)
==68196==    by 0x584E54F: __pyx_pw_6rtmidi_7_rtmidi_6MidiIn_13get_message(_object*, _object*) (_rtmidi.cpp:8133)
==68196==    by 0x53578C: ??? (in /usr/bin/python3.9)
==68196==    by 0x516542: _PyEval_EvalFrameDefault (in /usr/bin/python3.9)
==68196==    by 0x514A74: ??? (in /usr/bin/python3.9)
==68196==    by 0x51480A: _PyEval_EvalCodeWithName (in /usr/bin/python3.9)
==68196==  Address 0x4ca44b4 is 84 bytes inside a block of size 232 alloc'd
==68196==    at 0x4845033: operator new(unsigned long) (in /usr/libexec/valgrind/vgpreload_helgrind-amd64-linux.so)
==68196==    by 0x58558C1: RtMidiIn::openMidiApi(RtMidi::Api, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int) (RtMidi.cpp:416)
==68196==    by 0x5855A20: RtMidiIn::RtMidiIn(RtMidi::Api, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int) (RtMidi.cpp:450)
==68196==    by 0x5848DA9: __pyx_pf_6rtmidi_7_rtmidi_6MidiIn___cinit__ (_rtmidi.cpp:7400)
==68196==    by 0x5848DA9: __pyx_pw_6rtmidi_7_rtmidi_6MidiIn_1__cinit__ (_rtmidi.cpp:7287)
==68196==    by 0x5848DA9: __pyx_tp_new_6rtmidi_7_rtmidi_MidiIn(_typeobject*, _object*, _object*) (_rtmidi.cpp:10705)
==68196==    by 0x521B55: _PyObject_MakeTpCall (in /usr/bin/python3.9)
==68196==    by 0x51B9F7: _PyEval_EvalFrameDefault (in /usr/bin/python3.9)
==68196==    by 0x514A74: ??? (in /usr/bin/python3.9)
==68196==    by 0x51480A: _PyEval_EvalCodeWithName (in /usr/bin/python3.9)
==68196==    by 0x5FB256: PyEval_EvalCode (in /usr/bin/python3.9)
==68196==    by 0x6205FA: ??? (in /usr/bin/python3.9)
==68196==    by 0x61B723: ??? (in /usr/bin/python3.9)
==68196==    by 0x61FB2C: ??? (in /usr/bin/python3.9)
==68196==  Block was alloc'd by thread #1

with something like:

#! /usr/bin/python3

import rtmidi
import time

midiin = rtmidi.MidiIn()

ports = midiin.get_ports()

nr = ports.index('CH345:CH345 MIDI 1 20:0')
midiin.open_port(nr)

while True:
    print('bla', midiin.get_message())
    time.sleep(0.1)

using:

valgrind --tool=helgrind python3 test3.py

@SpotlightKid
Copy link
Owner

Thanks for your PR and sorry for the slow response. I currently have only little time for FLOSS development but I'll look at your change as soon as I can.

@SpotlightKid
Copy link
Owner

It might be better, though, to file this PR upstream (https://github.com/thestk/rtmidi). Upstream is even slower at incorporating PRs, though, so including the change in my fork first is a possibility.

@SpotlightKid SpotlightKid self-assigned this Aug 24, 2023
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.

None yet

2 participants