Skip to content

Commit

Permalink
Stop the pipeline in __del__ to avoid Python hangs at exit (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok authored Jul 28, 2024
1 parent 2126f9a commit 230cb00
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/spdl/dataloader/_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ def __init__(
def __str__(self) -> str:
return self._str

def __del__(self) -> None:
"""Stop the pipeline if running."""
if _EventLoopState.STARTED <= self._event_loop_state < _EventLoopState.STOPPED:
warnings.warn(
f"Pipeline ({repr(self)}) is running in the background, but "
"there is no valid reference pointing the foreground object. "
"Stopping the background thread. "
"It is strongly advised to stop the pipeline explicity, "
"using the `auto_stop` context manager. "
"If you are using a framework and you cannot use the "
"context manager, try calling `stop` in done callback and "
"error callback."
)
self.stop()

def start(self, *, timeout: float | None = None, **kwargs) -> None:
"""Start the pipeline in background thread.
Expand Down
18 changes: 18 additions & 0 deletions tests/spdl_unittest/dataloader/pipeline_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import time
from contextlib import asynccontextmanager
from multiprocessing import Process

import pytest

Expand Down Expand Up @@ -1312,3 +1313,20 @@ def test_pipeline_stop_multiple_times():
pipeline.stop()
pipeline.stop()
pipeline.stop()


def _run_pipeline_without_closing():
pipeline = PipelineBuilder().add_source(range(10)).add_sink(1).build()
pipeline.start()


def test_pipeline_no_close():
"""Python interpreter can terminate even when Pipeline is not explicitly closed."""

p = Process(target=_run_pipeline_without_closing)
p.start()
p.join(timeout=10)

if p.exitcode is None:
p.kill()
raise RuntimeError("Process did not self-terminate.")

0 comments on commit 230cb00

Please sign in to comment.