Skip to content

Commit

Permalink
Check for signals being reset to our handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
sternj committed Mar 19, 2024
1 parent 59ddee8 commit 34f4cef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion scalene/replacement_signal_fns.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

@Scalene.shim
def replacement_signal_fns(scalene: Scalene) -> None:

scalene_signals = scalene.get_signals()
expected_handlers_map = {
scalene_signals.malloc_signal: scalene.malloc_signal_handler,
scalene_signals.free_signal: scalene.free_signal_handler,
scalene_signals.memcpy_signal: scalene.memcpy_signal_handler,
signal.SIGTERM: scalene.term_signal_handler,
scalene_signals.cpu_signal: scalene.cpu_signal_handler,
}
old_signal = signal.signal
if sys.version_info < (3, 8):

Expand Down Expand Up @@ -55,6 +62,9 @@ def replacement_signal(signum: int, handler: Any) -> Any:
# previous handler, so this behavior is reasonable
if signal in all_signals and (handler is signal.SIG_IGN or handler is signal.SIG_DFL):
return handler
# If trying to "reset" to a handler that we already set it to, ignore
if signum in expected_handlers_map and expected_handlers_map[signum] is handler:
return signal.SIG_IGN
if signum in all_signals:
print(
"Error: Scalene cannot profile your program because it (or one of its packages)\n"
Expand Down
5 changes: 4 additions & 1 deletion scalene/scalene_profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ def last_profiled_tuple() -> Tuple[Filename, LineNumber, ByteCodeIndex]:
def get_original_lock() -> threading.Lock:
"""Return the true lock, which we shim in replacement_lock.py."""
return Scalene.__original_lock()

@staticmethod
def get_signals():
return Scalene.__signals
# when did we last receive a signal?
__last_signal_time = TimeInfo()

Expand Down Expand Up @@ -1597,6 +1599,7 @@ def stop() -> None:
pywhere.set_scalene_done_true()



Scalene.disable_signals()
Scalene.__stats.stop_clock()
if Scalene.__args.outfile:
Expand Down

0 comments on commit 34f4cef

Please sign in to comment.