Skip to content

Commit 89525a1

Browse files
committed
gh-151029: Tidy deleted mapping remote exec tests
1 parent fb1245c commit 89525a1

2 files changed

Lines changed: 21 additions & 25 deletions

File tree

Lib/test/test_sys.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,19 @@ def _run_remote_exec_test(self, script_code, python_args=None, env=None,
20862086
proc.terminate()
20872087
proc.wait(timeout=SHORT_TIMEOUT)
20882088

2089+
def _run_remote_exec_with_deleted_mapping(self, deleted_path, **kwargs):
2090+
def delete_loaded_mapping(proc):
2091+
os_helper.unlink(deleted_path)
2092+
with open(f'/proc/{proc.pid}/maps', encoding='utf-8') as maps:
2093+
self.assertIn(f'{deleted_path} (deleted)', maps.read())
2094+
2095+
script = 'print("Remote script executed successfully!")'
2096+
returncode, stdout, stderr = self._run_remote_exec_test(
2097+
script, after_ready=delete_loaded_mapping, **kwargs)
2098+
self.assertEqual(returncode, 0)
2099+
self.assertIn(b"Remote script executed successfully!", stdout)
2100+
self.assertEqual(stderr, b"")
2101+
20892102
def test_remote_exec(self):
20902103
"""Test basic remote exec functionality"""
20912104
script = 'print("Remote script executed successfully!")'
@@ -2239,18 +2252,8 @@ def test_remote_exec_deleted_libpython(self):
22392252
env['LD_LIBRARY_PATH'] = lib_dir if not ld_library_path else (
22402253
lib_dir + os.pathsep + ld_library_path)
22412254

2242-
def delete_loaded_libpython(proc):
2243-
os_helper.unlink(copied_libpython)
2244-
with open(f'/proc/{proc.pid}/maps', encoding='utf-8') as maps:
2245-
self.assertIn(f'{copied_libpython} (deleted)',
2246-
maps.read())
2247-
2248-
script = 'print("Remote script executed successfully!")'
2249-
returncode, stdout, stderr = self._run_remote_exec_test(
2250-
script, env=env, after_ready=delete_loaded_libpython)
2251-
self.assertEqual(returncode, 0)
2252-
self.assertIn(b"Remote script executed successfully!", stdout)
2253-
self.assertEqual(stderr, b"")
2255+
self._run_remote_exec_with_deleted_mapping(copied_libpython,
2256+
env=env)
22542257

22552258
@unittest.skipUnless(sys.platform == 'linux', 'Linux-only regression test')
22562259
@unittest.skipUnless(
@@ -2287,19 +2290,9 @@ def test_remote_exec_deleted_static_executable(self):
22872290
os.path.basename(sys.executable))
22882291
shutil.copy2(sys.executable, copied_python)
22892292

2290-
def delete_loaded_executable(proc):
2291-
os_helper.unlink(copied_python)
2292-
with open(f'/proc/{proc.pid}/maps', encoding='utf-8') as maps:
2293-
self.assertIn(f'{copied_python} (deleted)',
2294-
maps.read())
2295-
2296-
script = 'print("Remote script executed successfully!")'
2297-
returncode, stdout, stderr = self._run_remote_exec_test(
2298-
script, python_args=['-S'], python_executable=copied_python,
2299-
after_ready=delete_loaded_executable)
2300-
self.assertEqual(returncode, 0)
2301-
self.assertIn(b"Remote script executed successfully!", stdout)
2302-
self.assertEqual(stderr, b"")
2293+
self._run_remote_exec_with_deleted_mapping(
2294+
copied_python, python_args=['-S'],
2295+
python_executable=copied_python)
23032296

23042297
def test_remote_exec_in_process_without_debug_fails_envvar(self):
23052298
"""Test remote exec in a process without remote debugging enabled"""

Python/remote_debug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,9 @@ scan_linux_mapping_for_pyruntime_cookie(
857857
if (_Py_RemoteDebug_HasPermissionError()) {
858858
goto exit;
859859
}
860+
// A candidate mapping can disappear or contain unreadable holes while
861+
// the target process keeps running. Treat those as non-matches and
862+
// keep scanning other candidate mappings.
860863
PyErr_Clear();
861864
}
862865
else {

0 commit comments

Comments
 (0)