From f23af64b7de8618a58e46ab48181d4968692e80c Mon Sep 17 00:00:00 2001 From: Ferryyer <217455109+Ferryyer@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:28:24 -0800 Subject: [PATCH] LS-65: guard procfs tests and close Store SQLite connections Co-authored-by: multica-agent --- gpuqueue/core.py | 11 ++++++++--- tests/test_stop.py | 5 +++++ tests/test_store.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/test_store.py diff --git a/gpuqueue/core.py b/gpuqueue/core.py index 355b13a..1da6c4e 100644 --- a/gpuqueue/core.py +++ b/gpuqueue/core.py @@ -163,11 +163,16 @@ def __init__(self, root=DEFAULT_ROOT): connection.execute('CREATE TABLE IF NOT EXISTS tasks (id TEXT PRIMARY KEY,digest TEXT NOT NULL,doc TEXT NOT NULL,public TEXT NOT NULL,internal TEXT NOT NULL)') connection.execute('CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY,value TEXT NOT NULL)') + @contextlib.contextmanager def connect(self): connection = sqlite3.connect(self.root / 'queue.sqlite3', timeout=30) - connection.row_factory = sqlite3.Row - connection.execute('PRAGMA synchronous=FULL') - return connection + try: + connection.row_factory = sqlite3.Row + connection.execute('PRAGMA synchronous=FULL') + with connection: + yield connection + finally: + connection.close() def tasks(self): with self.connect() as connection: diff --git a/tests/test_stop.py b/tests/test_stop.py index a8c4c2e..f58acdc 100644 --- a/tests/test_stop.py +++ b/tests/test_stop.py @@ -32,6 +32,7 @@ def tearDown(self): def approve_claim(self): state=self.control.start(self.task['id'],self.task['digest']) self.control.claim(self.task['id'],self.task['digest'],state['revision']) + @unittest.skipUnless(Path('/proc/self/status').is_file(), 'Requires Linux /proc') def test_process_identity_and_parent_use_signal_visible_pids(self): process=subprocess.Popen(['/usr/bin/python3','-c','import time;time.sleep(30)']) self.processes.append(process) @@ -41,6 +42,7 @@ def test_process_identity_and_parent_use_signal_visible_pids(self): self.assertEqual(value['ppid'],os.getpid()) self.assertIn(process.pid,stop.table()) self.assertIsNone(stop.identity(-1)) + @unittest.skipUnless(Path('/proc/self/status').is_file(), 'Requires Linux /proc') def test_other_namespace_pid_collision_is_rejected(self): own = Path('/proc/self/status').read_text() mounted_pid = next(int(line.split()[1]) for line in own.splitlines() if line.startswith('Pid:')) @@ -89,6 +91,7 @@ def test_queued_stop_cannot_apply_for_gpu_or_submit_a_job(self): platform=Mock();platform.resources={};service=Service({},self.store,platform) service.tick();platform.request.assert_not_called();platform.submit_job.assert_not_called();platform.select.assert_not_called() self.assertEqual(self.store.get(self.task['id'])['public']['state'],'cancelled') + @unittest.skipUnless(Path('/proc/self/status').is_file(), 'Requires Linux /proc') @unittest.skipUnless(hasattr(os,'pidfd_open') and hasattr(signal,'pidfd_send_signal'),'pidfds unavailable') def test_legacy_stop_kills_its_tree_but_preserves_unrelated_process_and_outputs(self): self.approve_claim();p,child,argv=self.cpu_tree() @@ -100,6 +103,7 @@ def test_legacy_stop_kills_its_tree_but_preserves_unrelated_process_and_outputs( p.wait(timeout=5);self.assertIsNone(stop.identity(child));self.assertIsNone(unrelated.poll()) self.assertEqual(proof['state'],'stopped');self.assertEqual((self.folder/'outputs/kept.txt').read_text(),'existing result') self.assertFalse((self.folder/'outputs/next-step.txt').exists()) + @unittest.skipUnless(Path('/proc/self/status').is_file(), 'Requires Linux /proc') @unittest.skipUnless(hasattr(os,'pidfd_open') and hasattr(signal,'pidfd_send_signal'),'pidfds unavailable') def test_pid_or_command_mismatch_is_not_signalled(self): self.approve_claim();p,child,argv=self.cpu_tree();self.control.stop(self.task['id'],self.task['digest']) @@ -111,6 +115,7 @@ def test_pid_or_command_mismatch_is_not_signalled(self): # Explicit fixture cleanup of its own detached child. value=stop.identity(child) if value:stop.send(value,signal.SIGKILL) + @unittest.skipUnless(Path('/proc/self/status').is_file(), 'Requires Linux /proc') @unittest.skipUnless(hasattr(os,'pidfd_open') and hasattr(signal,'pidfd_send_signal'),'pidfds unavailable') def test_new_supervisor_stops_current_step_without_mutating_snapshot(self): # CPU stand-in for a frozen runner, used only in this isolated test. diff --git a/tests/test_store.py b/tests/test_store.py new file mode 100644 index 0000000..331ac0b --- /dev/null +++ b/tests/test_store.py @@ -0,0 +1,33 @@ +"""SQLite transaction and connection lifetime guarantees.""" +from pathlib import Path +import sqlite3 +import tempfile +import unittest + +from gpuqueue.core import Store + + +class StoreConnectionTests(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + self.store = Store(Path(self.temp.name) / 'state') + + def assert_closed(self, connection): + with self.assertRaises(sqlite3.ProgrammingError): + connection.execute('SELECT 1') + + def test_success_commits_and_closes_connection(self): + with self.store.connect() as connection: + self.assertIsInstance(connection.execute('SELECT 1').fetchone(), sqlite3.Row) + connection.execute('INSERT INTO meta VALUES (?, ?)', ('committed', 'true')) + self.assert_closed(connection) + self.assertTrue(self.store.metadata('committed')) + + def test_exception_rolls_back_and_closes_connection(self): + with self.assertRaisesRegex(RuntimeError, 'abort transaction'): + with self.store.connect() as connection: + connection.execute('INSERT INTO meta VALUES (?, ?)', ('rolled_back', 'true')) + raise RuntimeError('abort transaction') + self.assert_closed(connection) + self.assertIsNone(self.store.metadata('rolled_back'))