Repository: sagemath/sage

Issue:
Doctest failures with alarm on OS X
### Steps To Reproduce

With OS X 13.6.3 (Intel) and OS X 14.2 (Apple M2), Sage 10.3.beta2 and later, `make ptest` and `make ptestlong` produce failures that were not present with 10.3.beta1. 

### Expected Behavior

Tests which used to pass should pass.

### Actual Behavior

As noted, tests do not pass. The end of `make ptestlong` reports:
```
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/libs/flint/nmod_poly_linkage.pxi  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/libs/gap/element.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/interfaces/gap.py  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/geometry/integral_points.pxi  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/libs/libecm.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/coding/linear_code.py  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/rings/factorint_pari.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/rings/complex_arb.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/rings/integer.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/rings/polynomial/polynomial_zmod_flint.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/rings/polynomial/polynomial_element.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/parallel/decorate.py  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/sets/recursively_enumerated_set.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/structure/coerce_actions.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/schemes/elliptic_curves/descent_two_isogeny.pyx  # Killed due to alarm
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/libs/giac/__init__.py  # Timed out
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/doctest/forker.py  # 5 doctests failed
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/tests/books/computational-mathematics-with-sagemath/mpoly_doctest.py  # 1 doctest failed
sage -t --random-seed=226393960629615338750434927109379046589 src/sage/doctest/control.py  # 8 doctests failed
```
The first failure, for example:
```
sage -t --long --random-seed=16301058625430036409274889310772014116 src/sage/libs/flint/nmod_poly_linkage.pxi
    Killed due to alarm
**********************************************************************
Tests run before process (pid=50812) failed:
sage: P.<x> = GF(32003)[] ## line 40 ##
sage: Q.<x> = GF(7)[] ## line 42 ##
sage: sig_on_count() # check sig_on/off pairings (virtual doctest) ## line 43 ##
0
sage: P.<x> = GF(32003)[] ## line 50 ##
sage: del x ## line 51 ##
sage: Q.<x> = GF(7)[] ## line 53 ##
sage: del x ## line 54 ##


  [snip]


sage: f^2 % g ## line 521 ##
15328*x + 6968
sage: pow(f, -2, g) ## line 524 ##
16346/(x + 251)
sage: (f^2 % g)^-1 ## line 526 ##
16346/(x + 251)
sage: pow(f, 5, g) ## line 529 ##
7231*x + 17274
sage: f^5 % g ## line 531 ##
7231*x + 17274
sage: n = 2^23 ## line 536 ##
sage: alarm(0.2); x^n; cancel_alarm() ## line 537 ##
```

### Additional Information

- I believe the failures are due to #36640. `git bisect` led me to that, and then independently 10.3.beta1 + that PR led to the failures.
- I have been unable to reproduce these failures using `sage -tp`. 
- Tests fail with `make test`, `make ptest`, `make ptestlong`.

### Environment

```markdown
- **OS**: OS X 13.6.3 and OS X 14.2, latest Xcode command-line tools
- **Sage Version**: 10.3.beta2
- as many homebrew packages installed as Sage requests
```


### Checklist

- [X] I have searched the existing issues for a bug report that matches the one I want to file, without success.
- [X] I have read the documentation and troubleshoot guide


Discussion:
Thanks for opening this. 
I can confirm that this was introduced in 10.3.beta2: 
- I see these failures in the CI MacOS for 10.3.beta2 (in [stage-2 / local-macos (latest, homebrew-macos-usrlocal-minimal)](https://github.com/sagemath/sage/actions/runs/7202678228/job/19631749158#logs), https://github.com/sagemath/sage/actions/runs/7202678228/job/19631749158#step:9:20392)
- not present in 10.3.beta1 runs (e.g. https://github.com/sagemath/sage/actions/runs/7157804484/job/19492023218)


The tokenpool code does play with SIGALRM. https://github.com/milahu/gnumake-tokenpool/blob/main/py/src/gnumake_tokenpool/tokenpool.py#L178
I'll try to prepare a fix
#36948 has a tentative solution. I haven't been able to reproduce the failure yet locally, so if you have a chance, please try
No, it doesn't fix it. 
> No, it doesn't fix it.

Confirmed, doesn't fix it for me either.
Looks like I may have a solution that uses cysignals in combination with tokenpool.

Resolving patch:
diff --git a/src/sage/doctest/forker.py b/src/sage/doctest/forker.py
--- a/src/sage/doctest/forker.py
+++ b/src/sage/doctest/forker.py
@@ -1806,12 +1806,13 @@ def parallel_dispatch(self):
         job_client = None
         try:
             from gnumake_tokenpool import JobClient, NoJobServer
+        except ImportError:
+            pass
+        else:
             try:
-                job_client = JobClient()
+                job_client = JobClient(use_cysignals=True)
             except NoJobServer:
                 pass
-        except ImportError:
-            pass
 
         source_iter = iter(self.controller.sources)
 


Test patch: