Skip to content

Commit 7125d16

Browse files
committed
Fix minor bugs with queue and teardown
1 parent 533dad0 commit 7125d16

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ __pycache__
66
.eggs
77
/dist/
88
/wheelhouse/
9+
.DS_Store

‎portaudio‎

Submodule portaudio updated 230 files

‎src/rtmixer.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ int callback(const void* input, void* output, frame_t frameCount
128128
struct state* state = userData;
129129
CALLBACK_ASSERT(state);
130130

131-
memset(output, 0, sizeof(float) * state->output_channels * frameCount);
131+
if (output) // NULL for input-only streams
132+
{
133+
memset(output, 0, sizeof(float) * state->output_channels * frameCount);
134+
}
132135

133136
get_stats(frameCount, statusFlags, &(state->stats));
134137

‎src/rtmixer.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def __init__(self, kind, qsize=16, **kwargs):
4141
self._actions = {}
4242
self._temp_action_ptr = _ffi.new('struct action**')
4343

44+
def __del__(self):
45+
# The C callback uses memory owned by this object, so the stream
46+
# must be closed before that memory is freed.
47+
try:
48+
if not self.closed:
49+
self.close()
50+
except Exception:
51+
pass # e.g. __init__() failed or interpreter shutdown
52+
4453
@property
4554
def actions(self):
4655
"""The set of active "actions"."""
@@ -76,7 +85,8 @@ def cancel(self, action, time=0, allow_belated=True):
7685
requested_time=time,
7786
action=action,
7887
))
79-
self._enqueue(cancel_action)
88+
# The callback dereferences *action*, so it must be kept alive:
89+
self._enqueue(cancel_action, keep_alive=action)
8090
return cancel_action
8191

8292
def fetch_and_reset_stats(self, time=0, allow_belated=True):
@@ -126,6 +136,10 @@ def _check_channels(self, channels, kind):
126136

127137
def _enqueue(self, action, keep_alive=None):
128138
self._drain_result_q()
139+
# Each action ends up in the result queue exactly once, so limiting
140+
# the number of pending actions guarantees that it never overflows:
141+
if len(self._actions) >= len(self._result_q):
142+
raise RuntimeError('Too many pending actions')
129143
self._temp_action_ptr[0] = action
130144
ret = self._action_q.write(self._temp_action_ptr)
131145
if ret != 1:

0 commit comments

Comments
 (0)