Skip to content

fix: sea-state batch workers write their own numba cache - #48

Merged
SMI-Lab-Inha merged 5 commits into
devfrom
fix/batch-numba-cache
Nov 19, 2026
Merged

SMI-Lab-Inha merged 5 commits into
devfrom
fix/batch-numba-cache

Conversation

@SMI-Lab-Inha

@SMI-Lab-Inha SMI-Lab-Inha commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

Summary

run_sea_state_batch(..., options=SeaStateBatchOptions(workers=N)) with N > 1 started its spawn workers with the caller's environment, so all of them wrote one numba cache directory. That cache is not safe for concurrent writers. IndexDataCacheFile.save (numba/core/caching.py):

  1. reads the index;
  2. picks the first data-file number not in it (<kernel>.1.nbc, .2.nbc, ...);
  3. saves the index;
  4. saves the data.

Two workers that compile the same kernel for different signatures at the same time can both pick .1.nbc. The final index can then name one signature while the file holds the other's machine code, and a later process that loads that entry runs code compiled for different argument types.

Change

Each worker writes a numba cache of its own, and that cache persists across batches.

Before After
Worker cache the caller's slot folder slot-<i> under cablexsec/numba-batch in the user cache directory (%LOCALAPPDATA% on Windows, ~/Library/Caches on macOS, $XDG_CACHE_HOME or ~/.cache elsewhere); CABLEXSEC_BATCH_CACHE_DIR overrides it
One writer per cache no each worker takes its slot's lock file itself, as its first task (_claim_slot: non-blocking exclusive msvcrt.locking / fcntl.flock). It holds the lock for its whole lifetime, so the slot stays taken even if the caller ends first; the operating system releases it when the worker ends.
Two batches on one machine shared one cache the caller only checks that a slot looks free before it starts a worker there. If another process takes the slot in between, the claim fails, that worker is shut down and the next slot is tried (16 in total). A worker without a free slot, or with an unwritable folder, gets a temporary cache that is removed after every pool has shut down.
Workers one ProcessPoolExecutor(max_workers=N) N one-process spawn pools (_workers), each started while NUMBA_CACHE_DIR names its slot
Scheduling pool.map each worker takes the next run when it is free; outcomes in run order

The caller's NUMBA_CACHE_DIR is restored in every case, including a worker that fails to start; the pools already started are then shut down. The tests point CABLEXSEC_BATCH_CACHE_DIR at a folder of the test process (tests/conftest.py), so they never write to the user cache directory.

Wall time (local, Windows, 4 runs, workers = 2, final code)

Batch Wall time
first (empty slots) 5.0 s
second (slots kept) 3.0 s
in-process (workers = 1) 0.6 s

Both worker batches are bit-identical to the in-process batch (largest relative difference 0). On a batch this small, the cost is mostly the spawn and import of the workers; the persistent slots save the recompilation, here 2 s. workers = 4 was not measured: this machine is limited to two parallel processes.

Tests

tests/test_sea_state_batch_workers.py (no taxonomy marker, so the report's taxonomy counts do not change):

  • every worker sees its own NUMBA_CACHE_DIR, also when workers are started from several threads at once (the change of the process environment is serialised), and the caller's value is restored;
  • a cold batch on two workers writes each worker's kernels into its own slot. A second batch reuses the slots and compiles nothing: the modification times of every .nbi index file are unchanged.
  • Both batches equal the in-process batch exactly when every process runs BLAS on one thread, as in CI. Otherwise the test falls back to rtol=1e-10 and states the reason: threaded BLAS reductions in the calling process differ at round-off.
  • the worker, not the caller, holds its slot while it runs, and a second claim is refused;
  • a batch beside another (slot 0 held) gets slots 1 and 2, which are taken while their workers run and free afterwards;
  • a slot taken by another process between the check and the worker's claim is skipped;
  • with 15 of 16 slots held, a worker gets the last slot and the others temporary caches, which are removed afterwards;
  • an unusable cache folder falls back to temporary caches;
  • the folder follows the override and the platform;
  • a worker that fails to start leaves no pool running.

All of test_sea_state_batch.py and test_sea_state_batch_inputs.py pass, also with CABLEXSEC_DISABLE_NUMBA=1. mypy reports no issues; ruff check and ruff format pass.

@SMI-Lab-Inha

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T11:30:51.248706Z fc12715 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@read-the-docs-community

read-the-docs-community Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 CableXSec | 🛠️ Build #36011143 | 📁 Comparing fc12715 against latest (240bf9c)

  🔍 Preview build  

24 files changed · ± 24 modified

± Modified

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: b6b5abc494

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@SMI-Lab-Inha

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1d79224e92

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@SMI-Lab-Inha

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6ee9660c5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@SMI-Lab-Inha

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 020e544827

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@SMI-Lab-Inha

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 150ceae06e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

run_sea_state_batch with workers > 1 started its spawn workers with the
caller's environment, so all of them shared one numba cache directory. numba
numbers a new on-disk cache entry from the index it last read and saves the
index before the data (numba/core/caching.py, IndexDataCacheFile.save): two
workers compiling one kernel for different signatures at the same time could
both take the same data file and leave the index pointing at the other's
machine code, which a later process would load.

Each worker is now a one-process spawn pool started while NUMBA_CACHE_DIR
names its own directory in a temporary directory of the batch, removed
afterwards; runs go to whichever worker is free, as with the shared pool. A
worker that fails to start shuts down the pools already started, and the
caller's NUMBA_CACHE_DIR is restored in every case. Each worker compiles the
kernels it uses; results are unchanged.

tests/test_sea_state_batch_workers.py: each worker sees its own cache
directory; a cold-cache batch on two workers writes kernels into each
worker's own directory, leaves nothing behind and matches the in-process
batch; a failed start leaves no pool running.

Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
A temporary cache per worker made every batch recompile every kernel in
every worker. Worker i now writes the slot folder slot-<i> under
cablexsec/numba-batch in the user cache directory (LOCALAPPDATA on Windows,
~/Library/Caches on macOS, XDG_CACHE_HOME or ~/.cache elsewhere;
CABLEXSEC_BATCH_CACHE_DIR overrides it), which later batches reuse.

The batch holds a non-blocking exclusive lock file per slot (msvcrt or
flock; released by the operating system if the process ends), so one slot is
written by one process at a time. A batch beside another takes the next free
slots out of 16; a worker without a free slot, or with an unwritable folder,
gets a temporary cache that is removed afterwards.

The tests give the batch caches a folder of the test process. New tests:
slots are kept and a second batch compiles nothing (its index files are
unchanged); results equal the in-process batch exactly when every process
runs BLAS on one thread (as CI does), else to rtol 1e-10 with the reason
stated; concurrent batches take the next free slots; the temporary fallback;
an unusable folder; the platform folder and the override.

Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
The caller held the slot locks. If it was killed while a worker was still
compiling, the locks were released at once while the orphaned worker kept
writing its cache, and a later batch could take the same slot.

The worker now locks its slot itself (_claim_slot, its first task) and holds
it for its lifetime; the operating system releases it when the worker ends.
The caller only checks that a slot looks free before starting a worker on
it; if another process wins the slot meanwhile, the claim fails, that worker
is shut down and the next slot is tried. Temporary fallback caches are
removed after every pool has shut down.

Tests: the worker, not the caller, holds the slot while it runs; a slot taken
between the check and the claim is skipped.

Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
Two batches that start workers from different threads of one process could
interleave their changes of the process-wide NUMBA_CACHE_DIR, so that both
workers inherited the same cache directory and the caller's value was not
restored. _start_worker now holds a process-wide lock around the whole
save, set, spawn and restore sequence. Also rewraps a module docstring line
over the line limit.

Test: workers started from two threads at once each run with their own cache
directory, and the caller's value is restored.

Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
Signed-off-by: Jae Hoon Seo <jaehoon.seo@inha.ac.kr>
@SMI-Lab-Inha

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: fc12715bb2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant