8000 BUG: fixes np.random.zipf() hanging on pathological input by tylerjereddy · Pull Request #10912 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: fixes np.random.zipf() hanging on pathological input #10912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions numpy/random/mtrand/distributions.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
*/

#include "distributions.h"
#include <signal.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
Expand Down Expand Up @@ -718,12 +719,33 @@ double rk_wald(rk_state *state, double mean, double scale)
}
}

static void kb_interrupt_handler(int signum) {
/* signal handlers are extremely limited
* and attempting to raise a Python
* KeyboardInterrupt via the Python
* C API here causes a segfault
*/
printf("\nCaught Keyboard Interrupt\n");
exit(1);
}

long rk_zipf(rk_state *state, double a)
{
double am1, b;

am1 = a - 1.0;
b = pow(2.0, am1);

/* using PyErr_CheckSignals() from
* the Python C API would allow
* for keyboard interrupt handling
* BUT results in a segfault
* after Ctrl+C, so an alternative
* approach is used here;
* Related to Issue #9829
*/
signal(SIGINT, kb_interrupt_handler);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The handler is installed on every call to rk_zipf? When do you remove it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in the wrong place anyway. The problem is that the while loop never exits because the acceptance-rejection based implementation achieves near 100% rejection for parameters near one on account of an unfortunate scaling.


while (1) {
double T, U, V, X;

Expand Down
24 changes: 24 additions & 0 deletions numpy/random/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from numpy import random
import sys
import warnings
import signal
import pytest
import subprocess
import os


class TestSeed(object):
Expand Down Expand Up @@ -1634,3 +1638,23 @@ def test_three_arg_funcs(self):

out = func(self.argOne, self.argTwo[0], self.argThree)
assert_equal(out.shape, self.tgtShape)

def test_zipf_interrupt(tmpdir):
# test that zipf can be interrupted
# with i.e., KeyboardInterrupt
# for pathological inputs
# related to Issue #9829
p = str(tmpdir.mkdir("sub").join("zipf_pathological.py"))

with open(p, 'w') as tempfile:
tempfile.write("import numpy as np\n")
tempfile.write("np.random.zipf(1.0000000000001)\n")

process = subprocess.Popen(["python", "{script}".format(script=p)])
if os.name == 'nt':
process.send_signal(signal.CTRL_C_EVENT)
else:
process.send_signal(signal.SIGINT)
process.communicate()
ret_code = process.returncode
assert ret_code != 0
0