Skip to content

Commit

Permalink
Clean up some exit behaviors (prevent hanging final thread)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgribble committed Mar 7, 2025
1 parent e94cdbf commit a18838b
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
3 changes: 2 additions & 1 deletion mfp/builtins/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def offset(self, channel, start):

async def _init_file_read(self):
ready = asyncio.Event()
loop = asyncio.get_event_loop()

def _read_helper():
import soundfile as sf
Expand All @@ -113,7 +114,7 @@ def _read_helper():

self.file_len = self.file_data.shape[0]
self.file_ready = True
ready.set()
loop.call_soon_threadsafe(ready.set)

thread = Thread(target=_read_helper)
thread.start()
Expand Down
3 changes: 1 addition & 2 deletions mfp/mfp_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,14 @@ async def main():
log.log_force_console = True
await app.finish()

for thread in app.leftover_threads:
for thread in [*app.leftover_threads]:
thread.join()

if app.debug:
import yappi
yappi.stop()
yappi.convert2pstats(yappi.get_func_stats()).dump_stats('mfp-main-funcstats.pstats')


def main_sync_wrapper():
import asyncio
asyncio.run(main())
4 changes: 2 additions & 2 deletions mfp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ def wait_for_all(klass):
t for t in QuittableThread._all_threads
if t.is_alive()
]

if len(living_threads) > 0:
next_victim = living_threads[0]
else:
Expand All @@ -225,10 +224,11 @@ def wait_for_all(klass):
@classmethod
async def await_all(klass):
all_dead = asyncio.Event()
loop = asyncio.get_event_loop()

def _killer_thread():
klass.wait_for_all()
all_dead.set()
loop.call_soon_threadsafe(all_dead.set)

t = Thread(target=_killer_thread)
t.start()
Expand Down

0 comments on commit a18838b

Please sign in to comment.