Skip to content
Open
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
11 changes: 8 additions & 3 deletions gpuqueue/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:'))
Expand Down Expand Up @@ -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()
Expand All @@ -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'])
Expand All @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions tests/test_store.py
Original file line number Diff line number Diff line change
@@ -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'))
Loading