This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: On FreeBSD, signal.NSIG is smaller than biggest signal value
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: jgehrcke, neologix, osvenskan, pitrou, sdaoden, vstinner
Priority: normal Keywords: patch

Created on 2014-02-10 17:36 by jgehrcke, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
signal_nsig_freebsd.patch vstinner, 2014-05-22 13:05 review
signal_nsig_freebsd-2.patch vstinner, 2014-05-22 20:05 review
Messages (10)
msg210854 - (view) Author: Dr. Jan-Philip Gehrcke (jgehrcke) * Date: 2014-02-10 17:36
On FreeBSD, signal.NSIG is smaller than what the documentation promises: "One more than the number of the highest signal number".

On Linux, the highest numerical signal value is smaller/equal signal.NSIG (expected behavior):

>>> import signal
>>> signals = [s for s in dir(signal) if s.startswith("SIG")]
>>> max([(getattr(signal, s), s) for s in signals])
(64, 'SIGRTMAX')
>>> signal.NSIG
65

On FreeBSD (since version 7, when SIGRTMIN/MAX have been introduced), Python's signal.NSIG is either 32 (if defined by the system, depending on __BSD_VISIBLE, see http://svnweb.freebsd.org/base/head/sys/sys/signal.h?revision=233519&view=markup#l331) or 64 (if chosen by signalmodule.c as a fallback). In any case, on FreeBSD the numerical values of SIGRTMIN/MAX are 65 and 126 and therefore both greater than Python's signal.NSIG:
http://svnweb.freebsd.org/base/head/sys/sys/signal.h?revision=233519&view=markup#l117

Consequently, Python's signal module exposes a number NSIG which is not 'true'. Two disadvantages:

- signal.NSIG is just not meaningful on FreeBSD. It is part of the signal module's public interface, and should do what its documentation says: "One more than the number of the highest signal number".

- this might lead to unexpected behavior when for instance calling signal.signal(signal.SIGRTMAX, signal.SIG_DFL). This works on Linux, but fails with a ValueError on FreeBSD: raised directly by signalmodule.c, because sig_num >= NSIG, i.e. sig_num seemingly is an invalid signal number, although it is not (https://github.com/python/cpython/blob/3.3/Modules/signalmodule.c#L323). This is the reason why I became aware of this topic.


I see three arguments here:

- if the system does not provide NSIG via signal.h and Python's signalvalue makes the wrong guess (i.e. fallback to 64), then signalmodule.c would be to blame.

- if the system provides NSIG via signal.h and this is not the true maximum, then one could say that Python is not to blame.

- on the other hand, signalmodule.c is aware of all signals that it actively checked for and therefore could derive "One more than the number of the highest signal number" on its own via something in the lines of max(signal_values)+1.

Regarding the latter point: if Python misses to check for a valid signal on a certain platform, then this actively derived NSIG value would not be entirely correct, either, seen from the platform's perspective. But the signal module would then at least be consistent with itself.

In case of FreeBSD, I am actually not sure if signal.NSIG *is* provided by the system or determined by the fallback method in signalmodule.c (I can't get my hands on a FreeBSD machine at the moment).


What do you think?

Btw, parts of this have already been mentioned here: http://bugs.python.org/issue12060
msg210892 - (view) Author: Dr. Jan-Philip Gehrcke (jgehrcke) * Date: 2014-02-11 00:23
As a follow-up, relevant output from FreeBSD 9:

$ python
Python 2.7.5 (default, Dec 20 2013, 21:12:37)
[GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
>>> signals = [s for s in dir(signal) if s.startswith("SIG")]
>>> max((getattr(signal, s), s) for s in signals)
(126, 'SIGRTMAX')
>>> signal.NSIG
32
>>> signal.signal(signal.SIGRTMAX, lambda *a: None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: signal number out of range


Hence, it's not the fallback to 64, it's FreeBSD's signal.h telling that NSIG is 32.
msg218895 - (view) Author: Dr. Jan-Philip Gehrcke (jgehrcke) * Date: 2014-05-22 12:56
If you are thinking TL;DR:

This fails on FreeBSD:

>>> signal.signal(signal.SIGRTMAX, lambda *a: None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: signal number out of range

Although of infrequent use, I doubt that this is expected or desired behavior.
msg218896 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-05-22 13:05
The current implementation of _signal requires a limit on the number of signals to its internal array used to store Python callback:

static volatile struct {
    sig_atomic_t tripped;
    PyObject *func;
} Handlers[NSIG];

If you want to kill the arbitrary limit, you need to change this structure.

Maybe we need to find NSIG value differently on FreeBSD? For example try to use _SIG_MAXSIG.
http://lists.freebsd.org/pipermail/freebsd-doc/2010-August/017500.html

Please try attached on FreeBSD.
msg218913 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-05-22 19:12
> If you want to kill the arbitrary limit, you need to change this
> structure.

Or the structure could simply host up to 256 handlers, regardless of NSIG.
I'm uncomfortable with tweaking NSIG specifically for FreeBSD. If the FreeBSD headers export the wrong value, it's not really Python's problem.
msg218914 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2014-05-22 19:40
> Or the structure could simply host up to 256 handlers, regardless of NSIG.
> I'm uncomfortable with tweaking NSIG specifically for FreeBSD. If the FreeBSD headers export the wrong value, it's not really Python's problem.

Agreed.
And the current code is already complicated enough:

#ifndef NSIG
# if defined(_NSIG)
#  define NSIG _NSIG            /* For BSD/SysV */
# elif defined(_SIGMAX)
#  define NSIG (_SIGMAX + 1)    /* For QNX */
# elif defined(SIGMAX)
#  define NSIG (SIGMAX + 1)     /* For djgpp */
# else
#  define NSIG 64               /* Use a reasonable default value */
# endif
#endif
msg218916 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-05-22 20:05
Extract of system signal.h:
 
#if __BSD_VISIBLE
#define NSIG            32      /* number of old signals (counting 0) */
#endif

whereas <sys/_sigset.h> contains:

#define _SIG_MAXSIG     128

In signalmodule.c, NSIG is still important in the function sigset_to_set(): we need to have the exact maximum signal number of a sigset.

I prefer to make signalmodule.c a little big uglier to fix the NSIG value. I tested attached signal_nsig_freebsd-2.patch on FreeBSD 9.

I suggest to backport this fix to Python 2.7 and 3.4.
msg218954 - (view) Author: Dr. Jan-Philip Gehrcke (jgehrcke) * Date: 2014-05-23 09:57
We should match the unit test with the documentation for signal.NSIG. Either the code or the docs or both need to change.

Currently the docs say that signal.NSIG is "One more than the number of the highest signal number." ("https://docs.python.org/3.4/library/signal.html#signal.NSIG).

In case of FreeBSD's _SIG_MAXSIG (128) the documentation is still wrong: the highest signal value MAX is 126 (see http://bugs.python.org/issue20584#msg210892). According to the docs, NSIG should then be 127.

In signal_nsig_freebsd-2.patch the test `self.assertLess(max(signals), signal.NSIG)` tests for NSIG > MAX instead of for NSIG = MAX+1.

So, either 

- we count signals by ourselves and build our own value of NSIG, in which case we can guarantee that NSIG = MAX+1 or

- we rely on the platform header files for extracting NSIG, but must note in the docs that NSIG is not always MAX+1.

The current patch + a documentation change would implement the latter case.

What is the exact meaning of _SIG_MAXSIG, where is that meaning defined?
msg218958 - (view) Author: Steffen Daode Nurpmeso (sdaoden) Date: 2014-05-23 10:55
Salut!, amis français!  (Und auch sonst so, natürlich.)

POSIX has recently standardized a NSIG_MAX constant in <limits.h> [1]:

  The value of {NSIG_MAX} shall be no greater than the number of signals that the sigset_t type (see [cross-ref to <signal.h>]) is capable of representing, ignoring any restrictions imposed by sigfillset() or sigaddset().

I'm personally following an advise of Rich Felker in the meantime:

  #ifdef NSIG_MAX
  # undef NSIG
  # define NSIG           NSIG_MAX
  #elif !defined NSIG
  # define NSIG           ((sizeof(sigset_t) * 8) - 1)
  #endif

That is for "old" signals only, there; maybe reducing this to

  #undef NSIG
  #ifdef NSIG_MAX
  # define NSIG  NSIG_MAX
  #else
  # define NSIG  ((sizeof(sigset_t) * 8) - 1)
  #endif 

should do the trick for Python on any POSIX system?
Ciao from, eh, gray, Germany :)

[1] <http://austingroupbugs.net/view.php?id=741#c1834>
msg218984 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2014-05-23 18:32
> Jan-Philip Gehrcke added the comment:
>
> Currently the docs say that signal.NSIG is "One more than the number of the highest signal number." ("https://docs.python.org/3.4/library/signal.html#signal.NSIG).
>
> In case of FreeBSD's _SIG_MAXSIG (128) the documentation is still wrong: the highest signal value MAX is 126 (see http://bugs.python.org/issue20584#msg210892). According to the docs, NSIG should then be 127.

Yeah, but it doesn't matter.
We shouldn't be exposing this constant in the first place, all that
matters is that we accept all valid signals, and we don't crash whe
passing an invalid once (ssee below).

> Steffen Daode Nurpmeso added the comment:
>
>   #ifdef NSIG_MAX
>   # undef NSIG
>   # define NSIG           NSIG_MAX
>   #elif !defined NSIG
>   # define NSIG           ((sizeof(sigset_t) * 8) - 1)
>   #endif
>
> should do the trick for Python on any POSIX system?

This assumes that sigset_t is implemented as a raw bitmap, which isn't
documented (is could be implemented by an arbitrary  data structure).
On the other hand, it's really really likely, and should guarantee
that sigset & Co don't crash on too large values (or write to
arbitrary memory locations like fd_set when passed fd > FD_SETSIZE).

So I think we should go for the above patch (Steffen's), with a doc
update saying that "NSIG is a value larger than the largest signals".
If the OS headers don't provide it, it's not our business to try to
infer it.
History
Date User Action Args
2022-04-11 14:57:58adminsetgithub: 64783
2014-10-25 16:16:19osvenskansetnosy: + osvenskan
2014-05-23 18:32:36neologixsetmessages: + msg218984
2014-05-23 10:55:04sdaodensetmessages: + msg218958
2014-05-23 09:57:40jgehrckesetmessages: + msg218954
2014-05-22 20:05:13vstinnersetfiles: + signal_nsig_freebsd-2.patch

messages: + msg218916
versions: + Python 3.4, Python 3.5, - Python 3.1, Python 3.2, Python 3.3
2014-05-22 19:40:39neologixsetmessages: + msg218914
2014-05-22 19:13:00pitrousetnosy: + pitrou
messages: + msg218913
2014-05-22 13:05:24vstinnersetfiles: + signal_nsig_freebsd.patch
keywords: + patch
messages: + msg218896
2014-05-22 12:56:39jgehrckesetnosy: + vstinner
messages: + msg218895
2014-02-11 00:23:43jgehrckesetmessages: + msg210892
2014-02-10 17:36:38jgehrckecreate