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

🐛 Fix signal outside main thread #2333

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lamindb/_finish.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def save_run_logs(run: Run, save_run: bool = False) -> None:
)
artifact.save(upload=True, print_progress=False)
run.report = artifact
if save_run: # defaults to fast because is slow
if save_run: # defaults to false because is slow
run.save()


Expand Down
20 changes: 15 additions & 5 deletions lamindb/core/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hashlib
import signal
import sys
import threading
import traceback
from datetime import datetime, timezone
from pathlib import Path
Expand Down Expand Up @@ -126,8 +127,11 @@ def start(self, run: Run):
sys.stdout = LogStreamHandler(self.original_stdout, self.log_file)
sys.stderr = LogStreamHandler(self.original_stderr, self.log_file)
# handle signals
signal.signal(signal.SIGTERM, self.cleanup)
signal.signal(signal.SIGINT, self.cleanup)
# signal should be used only in the main thread, otherwise
# ValueError: signal only works in main thread of the main interpreter
if threading.current_thread() == threading.main_thread():
signal.signal(signal.SIGTERM, self.cleanup)
signal.signal(signal.SIGINT, self.cleanup)
# handle exceptions
sys.excepthook = self.handle_exception

Expand Down Expand Up @@ -241,6 +245,7 @@ def track(
params: dict | None = None,
new_run: bool | None = None,
path: str | None = None,
log_to_file: bool | None = None,
) -> None:
"""Initiate a run with tracked data lineage.

Expand All @@ -255,10 +260,13 @@ def track(
Args:
transform: A transform `uid` or record. If `None`, creates a `uid`.
params: A dictionary of parameters to track for the run.
new_run: If `False`, loads latest run of transform
(default notebook), if `True`, creates new run (default pipeline).
new_run: If `False`, loads the latest run of transform
(default notebook), if `True`, creates new run (default non-notebook).
path: Filepath of notebook or script. Only needed if it can't be
automatically detected.
log_to_file: If `True`, logs stdout and stderr to a file and
saves the file within the current run (default non-notebook),
if `False`, does not log the output (default notebook).

Examples:

Expand Down Expand Up @@ -349,7 +357,9 @@ def track(
)
self._run = run
track_environment(run)
if self.transform.type != "notebook":
if log_to_file is None:
log_to_file = self.transform.type != "notebook"
if log_to_file:
self._stream_tracker.start(run)
logger.important(self._logging_message_track)
if self._logging_message_imports:
Expand Down
Loading