From d8fde3850aae11d7b5d249b568983866e2f7a816 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 20 Aug 2026 10:36:45 -0500 Subject: [PATCH 1/3] mp3decoder inbuf update fix --- shared-module/audiomp3/MP3Decoder.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/shared-module/audiomp3/MP3Decoder.c b/shared-module/audiomp3/MP3Decoder.c index dc22c0be245..baf8ea26554 100644 --- a/shared-module/audiomp3/MP3Decoder.c +++ b/shared-module/audiomp3/MP3Decoder.c @@ -109,6 +109,8 @@ static void stream_set_blocking(audiomp3_mp3file_obj_t *self, bool block_ok) { mp_call_method_n_kw(1, 0, self->settimeout_args); } +static bool mp3file_update_inbuf_always_impl(audiomp3_mp3file_obj_t *self, bool block_ok); + /** Fill the input buffer unconditionally. * * Returns true if the input buffer contains any useful data, @@ -120,6 +122,13 @@ static void stream_set_blocking(audiomp3_mp3file_obj_t *self, bool block_ok) { * Sets self->eof if any read of the file returns 0 bytes */ static bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self, bool block_ok) { + background_callback_prevent(); + bool result = mp3file_update_inbuf_always_impl(self, block_ok); + background_callback_allow(); + return result; +} + +static bool mp3file_update_inbuf_always_impl(audiomp3_mp3file_obj_t *self, bool block_ok) { if (self->eof || INPUT_BUFFER_SPACE(self->inbuf) == 0) { return INPUT_BUFFER_AVAILABLE(self->inbuf) > 0; } @@ -135,7 +144,8 @@ static bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self, bool block self->inbuf.read_off = 0; } - for (size_t to_read; !self->eof && (to_read = INPUT_BUFFER_SPACE(self->inbuf)) > 0;) { + for (size_t to_read; !self->eof && INPUT_BUFFER_SPACE(self->inbuf) > 0 && + (to_read = (size_t)INPUT_BUFFER_SPACE(self->inbuf)) > 0;) { uint8_t *write_ptr = self->inbuf.buf + self->inbuf.write_off; ssize_t n_read = stream_read(self->stream, write_ptr, to_read); From 64fc089445416851281c5940fbfaf87107ff2a79 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 20 Aug 2026 12:06:42 -0500 Subject: [PATCH 2/3] remove extra function layer --- shared-module/audiomp3/MP3Decoder.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/shared-module/audiomp3/MP3Decoder.c b/shared-module/audiomp3/MP3Decoder.c index baf8ea26554..58a4106dd1a 100644 --- a/shared-module/audiomp3/MP3Decoder.c +++ b/shared-module/audiomp3/MP3Decoder.c @@ -109,8 +109,6 @@ static void stream_set_blocking(audiomp3_mp3file_obj_t *self, bool block_ok) { mp_call_method_n_kw(1, 0, self->settimeout_args); } -static bool mp3file_update_inbuf_always_impl(audiomp3_mp3file_obj_t *self, bool block_ok); - /** Fill the input buffer unconditionally. * * Returns true if the input buffer contains any useful data, @@ -123,12 +121,7 @@ static bool mp3file_update_inbuf_always_impl(audiomp3_mp3file_obj_t *self, bool */ static bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self, bool block_ok) { background_callback_prevent(); - bool result = mp3file_update_inbuf_always_impl(self, block_ok); - background_callback_allow(); - return result; -} -static bool mp3file_update_inbuf_always_impl(audiomp3_mp3file_obj_t *self, bool block_ok) { if (self->eof || INPUT_BUFFER_SPACE(self->inbuf) == 0) { return INPUT_BUFFER_AVAILABLE(self->inbuf) > 0; } @@ -170,7 +163,10 @@ static bool mp3file_update_inbuf_always_impl(audiomp3_mp3file_obj_t *self, bool } // Return true iff there are at least some useful bytes in the buffer - return INPUT_BUFFER_AVAILABLE(self->inbuf) > 0; + bool result = INPUT_BUFFER_AVAILABLE(self->inbuf) > 0; + + background_callback_allow(); + return result; } /** Update the inbuf from a background callback. From 8babf99236589f0288c3dd6c3ebc3061fdc1018b Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 20 Aug 2026 15:07:54 -0500 Subject: [PATCH 3/3] re-allow background tasks on other exit paths --- shared-module/audiomp3/MP3Decoder.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/shared-module/audiomp3/MP3Decoder.c b/shared-module/audiomp3/MP3Decoder.c index 58a4106dd1a..6f0b5d4723f 100644 --- a/shared-module/audiomp3/MP3Decoder.c +++ b/shared-module/audiomp3/MP3Decoder.c @@ -120,10 +120,13 @@ static void stream_set_blocking(audiomp3_mp3file_obj_t *self, bool block_ok) { * Sets self->eof if any read of the file returns 0 bytes */ static bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self, bool block_ok) { + // Every return from this function must be preceded by background_callback_allow(); background_callback_prevent(); if (self->eof || INPUT_BUFFER_SPACE(self->inbuf) == 0) { - return INPUT_BUFFER_AVAILABLE(self->inbuf) > 0; + bool result = INPUT_BUFFER_AVAILABLE(self->inbuf) > 0; + background_callback_allow(); + return result; } stream_set_blocking(self, block_ok); @@ -148,6 +151,7 @@ static bool mp3file_update_inbuf_always(audiomp3_mp3file_obj_t *self, bool block break; } self->eof = true; + background_callback_allow(); mp_raise_OSError(errcode); }