Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#13908 closed enhancement (fixed)

Exit Sage gracefully upon SIGHUP

Reported by: jdemeyer Owned by: jdemeyer
Priority: major Milestone: sage-5.7
Component: c_lib Keywords:
Cc: Merged in: sage-5.7.beta2
Authors: Jeroen Demeyer Reviewers: Volker Braun
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: #13748, #13946 Stopgaps:

Description (last modified by jdemeyer)

Change the signal handling code to allow a graceful forced exit of Sage. "graceful" meaning that code is interrupted and the normal exit handlers are run. This graceful exit is not guaranteed to work, code which is currently not interruptible would not be exited.

This might be used by the doctesting framework to handle timeouts.

This patch also improves interrupt debugging:

  1. Different debug levels, set in a C file such that not everything needs to be recompiled when changing the debug level.
  2. Show time between signal received and exception raised.

Apply: 13908_terminate.patch

Attachments (2)

13908_no_SIGTERM.patch (4.7 KB) - added by jdemeyer 7 years ago.
13908_terminate.patch (17.4 KB) - added by jdemeyer 7 years ago.

Download all attachments as: .zip

Change History (19)

comment:1 Changed 7 years ago by jdemeyer

  • Description modified (diff)
  • Summary changed from Exit Sage gracefully upon SIGHUP to Exit Sage gracefully upon SIGHUP/SIGTERM

comment:2 Changed 7 years ago by jdemeyer

  • Authors set to Jeroen Demeyer
  • Status changed from new to needs_review

comment:3 Changed 7 years ago by jdemeyer

  • Dependencies changed from #13748 to #13748, #13946

comment:4 Changed 7 years ago by jdemeyer

  • Status changed from needs_review to needs_work

comment:5 Changed 7 years ago by jdemeyer

  • Status changed from needs_work to needs_review

comment:6 Changed 7 years ago by vbraun

  • Reviewers set to Volker Braun
  • Status changed from needs_review to positive_review

Sounds good to me.

comment:7 Changed 7 years ago by jdemeyer

  • Milestone changed from sage-5.6 to sage-5.7

comment:8 Changed 7 years ago by jdemeyer

  • Status changed from positive_review to needs_work

See sage-devel.

comment:9 Changed 7 years ago by jdemeyer

  • Summary changed from Exit Sage gracefully upon SIGHUP/SIGTERM to Exit Sage gracefully upon SIGHUP

Since the main motivation is to have some way of gracefully terminating Sage, I'm handling only SIGHUP now, not SIGTERM.

comment:10 Changed 7 years ago by jdemeyer

  • Description modified (diff)

Changed 7 years ago by jdemeyer

Changed 7 years ago by jdemeyer

comment:11 Changed 7 years ago by jdemeyer

  • Description modified (diff)

comment:12 Changed 7 years ago by jdemeyer

  • Status changed from needs_work to needs_review

comment:13 Changed 7 years ago by vbraun

  • Status changed from needs_review to positive_review

Hmm I didn't see any discussion on sage-devel about SIGTERM but getting started with just catching SIGHUP is fine, too.

comment:15 Changed 7 years ago by jdemeyer

  • Merged in set to sage-5.7.beta2
  • Resolution set to fixed
  • Status changed from positive_review to closed

comment:16 follow-up: Changed 7 years ago by stumpc5

I was running a moinmoin wiki with sage. With this patch applied, that doesn't work anymore because of the new interruption handling in sage.ext._init_csage:

When running the wiki using

$ sage wikiserver.py

and importing sage.all.* therein, I get the following error:

    from sage.all import *
  File "/home/stumpc5/progs/sage-5.7/devel/sage-main/build/sage/all.py", line 59, in <module>
    _init_csage()
  File "c_lib.pyx", line 41, in sage.ext.c_lib._init_csage (sage/ext/c_lib.c:569)
    
ValueError: signal only works in main thread

The issue seems to be solved when I comment out the signal in _init_csage.

Question: Is there a (reasonable and finite) way to fix this issue properly? Can something serious go wrong if I comment these lines out?

Thanks, Christian

comment:17 in reply to: ↑ 16 Changed 7 years ago by jdemeyer

Replying to stumpc5:

Can something serious go wrong if I comment these lines out?

Not very serious. Interrupt handling might be a bit messed up, in particular some interrupts (CTRL-C) might be seen twice: once by the default Python handler (raise KeyboardInterrupt) and once by the special Sage handler (written in C).

The line

signal.signal(signal.SIGINT, sage_python_check_interrupt)

is mainly used to ensure that only the Sage signal handler is used.

But this will certainly have no consequences beyond interrupt handling.

Note: See TracTickets for help on using tickets.