Skip to content

[3.7] bpo-32604: Clean up created subinterpreters before runtime finalization. (gh-5709) #5710

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

Merged
merged 1 commit into from
Feb 17, 2018
Merged
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
4 changes: 4 additions & 0 deletions Include/internal/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ PyAPI_FUNC(void) _PyPathConfig_Clear(_PyPathConfig *config);

PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_LookUpID(PY_INT64_T);

PyAPI_FUNC(int) _PyInterpreterState_IDInitref(PyInterpreterState *);
PyAPI_FUNC(void) _PyInterpreterState_IDIncref(PyInterpreterState *);
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(PyInterpreterState *);


/* cross-interpreter data */

Expand Down
4 changes: 4 additions & 0 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
extern "C" {
#endif

#include "pythread.h"

/* This limitation is for performance and simplicity. If needed it can be
removed (with effort). */
#define MAX_CO_EXTRA_USERS 255
Expand Down Expand Up @@ -111,6 +113,8 @@ typedef struct _is {
struct _ts *tstate_head;

int64_t id;
int64_t id_refcount;
PyThread_type_lock id_mutex;

PyObject *modules;
PyObject *modules_by_index;
Expand Down
78 changes: 69 additions & 9 deletions Lib/test/test__xxsubinterpreters.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_subinterpreter(self):
interp = interpreters.create()
out = _run_output(interp, dedent("""
import _xxsubinterpreters as _interpreters
print(_interpreters.get_current())
print(int(_interpreters.get_current()))
"""))
cur = int(out.strip())
_, expected = interpreters.list_all()
Expand All @@ -172,7 +172,7 @@ def test_from_subinterpreter(self):
interp = interpreters.create()
out = _run_output(interp, dedent("""
import _xxsubinterpreters as _interpreters
print(_interpreters.get_main())
print(int(_interpreters.get_main()))
"""))
main = int(out.strip())
self.assertEqual(main, expected)
Expand All @@ -196,7 +196,7 @@ def test_from_subinterpreter(self):
interp = interpreters.create()
out = _run_output(interp, dedent(f"""
import _xxsubinterpreters as _interpreters
if _interpreters.is_running({interp}):
if _interpreters.is_running({int(interp)}):
print(True)
else:
print(False)
Expand All @@ -218,6 +218,63 @@ def test_bad_id(self):
interpreters.is_running(-1)


class InterpreterIDTests(TestBase):

def test_with_int(self):
id = interpreters.InterpreterID(10, force=True)

self.assertEqual(int(id), 10)

def test_coerce_id(self):
id = interpreters.InterpreterID('10', force=True)
self.assertEqual(int(id), 10)

id = interpreters.InterpreterID(10.0, force=True)
self.assertEqual(int(id), 10)

class Int(str):
def __init__(self, value):
self._value = value
def __int__(self):
return self._value

id = interpreters.InterpreterID(Int(10), force=True)
self.assertEqual(int(id), 10)

def test_bad_id(self):
for id in [-1, 'spam']:
with self.subTest(id):
with self.assertRaises(ValueError):
interpreters.InterpreterID(id)
with self.assertRaises(OverflowError):
interpreters.InterpreterID(2**64)
with self.assertRaises(TypeError):
interpreters.InterpreterID(object())

def test_does_not_exist(self):
id = interpreters.channel_create()
with self.assertRaises(RuntimeError):
interpreters.InterpreterID(int(id) + 1) # unforced

def test_repr(self):
id = interpreters.InterpreterID(10, force=True)
self.assertEqual(repr(id), 'InterpreterID(10)')

def test_equality(self):
id1 = interpreters.create()
id2 = interpreters.InterpreterID(int(id1))
id3 = interpreters.create()

self.assertTrue(id1 == id1)
self.assertTrue(id1 == id2)
self.assertTrue(id1 == int(id1))
self.assertFalse(id1 == id3)

self.assertFalse(id1 != id1)
self.assertFalse(id1 != id2)
self.assertTrue(id1 != id3)


class CreateTests(TestBase):

def test_in_main(self):
Expand Down Expand Up @@ -256,7 +313,7 @@ def test_in_subinterpreter(self):
out = _run_output(id1, dedent("""
import _xxsubinterpreters as _interpreters
id = _interpreters.create()
print(id)
print(int(id))
"""))
id2 = int(out.strip())

Expand All @@ -271,7 +328,7 @@ def f():
out = _run_output(id1, dedent("""
import _xxsubinterpreters as _interpreters
id = _interpreters.create()
print(id)
print(int(id))
"""))
id2 = int(out.strip())

Expand Down Expand Up @@ -365,7 +422,7 @@ def test_from_current(self):
script = dedent(f"""
import _xxsubinterpreters as _interpreters
try:
_interpreters.destroy({id})
_interpreters.destroy({int(id)})
except RuntimeError:
pass
""")
Expand All @@ -377,10 +434,10 @@ def test_from_sibling(self):
main, = interpreters.list_all()
id1 = interpreters.create()
id2 = interpreters.create()
script = dedent("""
script = dedent(f"""
import _xxsubinterpreters as _interpreters
_interpreters.destroy({})
""").format(id2)
_interpreters.destroy({int(id2)})
""")
interpreters.run_string(id1, script)

self.assertEqual(set(interpreters.list_all()), {main, id1})
Expand Down Expand Up @@ -699,11 +756,14 @@ def test_execution_namespace_is_main(self):
'spam': 42,
})

# XXX Fix this test!
@unittest.skip('blocking forever')
def test_still_running_at_exit(self):
script = dedent(f"""
from textwrap import dedent
import threading
import _xxsubinterpreters as _interpreters
id = _interpreters.create()
def f():
_interpreters.run_string(id, dedent('''
import time
Expand Down
Loading