Skip to content

Commit 647531f

Browse files
committed
Update tracing_new_process.py
apscheduler tries to schedule jobs when the interpreter is shutting down which can cause it to crash and leave us in a bad state
1 parent 5dffd2a commit 647531f

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

codeflash/tracing/tracing_new_process.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import threading
1414
import time
1515
from collections import defaultdict
16+
from importlib.util import find_spec
1617
from pathlib import Path
1718
from typing import TYPE_CHECKING, Any, Callable, ClassVar
1819

@@ -47,6 +48,18 @@ def __init__(self, code: FakeCode, prior: FakeFrame | None) -> None:
4748
self.f_locals: dict = {}
4849

4950

51+
def patch_ap_scheduler() -> None:
52+
if find_spec("apscheduler") is None:
53+
54+
import apscheduler.schedulers.background as bg
55+
import apscheduler.schedulers.blocking as bb
56+
from apscheduler.schedulers import base
57+
58+
bg.BackgroundScheduler.start = lambda _, *_a, **_k: None
59+
bb.BlockingScheduler.start = lambda _, *_a, **_k: None
60+
base.BaseScheduler.add_job = lambda _, *_a, **_k: None
61+
62+
5063
# Debug this file by simply adding print statements. This file is not meant to be debugged by the debugger.
5164
class Tracer:
5265
"""Use this class as a 'with' context manager to trace a function call.
@@ -839,6 +852,7 @@ def runctx(self, cmd: str, global_vars: dict[str, Any], local_vars: dict[str, An
839852
}
840853
args_dict["config"]["module_root"] = Path(args_dict["config"]["module_root"])
841854
args_dict["config"]["tests_root"] = Path(args_dict["config"]["tests_root"])
855+
patch_ap_scheduler()
842856
tracer = Tracer(
843857
config=args_dict["config"],
844858
output=Path(args_dict["output"]),

0 commit comments

Comments
 (0)