From 74cd0919709d084446ff83d77d2c288c4464d693 Mon Sep 17 00:00:00 2001 From: lemke_h Date: Fri, 18 Sep 2026 14:40:49 +0200 Subject: [PATCH] Fix BsStreamCounter.last_arrays becoming unusable after storing Found by running a real scan against a real bs stream on saresb-vcons-03 (a test eco.status_server + a real BsStreamCounter), then reading last_arrays back afterward, mirroring exactly what a user would do interactively - arr._scan_parameter raised deep inside h5py ("invalid identifier type to function"). _store_arrays() copied CounterValue.store_arrays's reload-then-close pattern verbatim ("re-open read-back handles so last_arrays keeps working after the write file closes"), but escape.Array's own dataset wrapper (ArrayH5Dataset) is lazy for at least some attributes even past load_from_result_file (default lazy_loading=False notwithstanding) - so the *reloaded* handles this assigned into self.last_arrays were themselves tied to a second h5py.File that gets closed two lines later, just as unusable as what it was replacing. There was never a need to reload in the first place: the arrays already in self.last_arrays (from _build_arrays) are plain in-memory, numpy-backed Array objects with no file dependency at all. Dropped the reload entirely; verified against the real recorded data (real scan parameter values, correct shape) both immediately and via a fresh DataSet.load_from_result_file() in a separate process. Co-Authored-By: Claude Sonnet 5 --- eco/detector/bs_counter.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/eco/detector/bs_counter.py b/eco/detector/bs_counter.py index 59f8c93..7f9fe9b 100644 --- a/eco/detector/bs_counter.py +++ b/eco/detector/bs_counter.py @@ -605,7 +605,11 @@ class BsStreamCounter: handling), just scoped to this one counter's channels rather than a whole scan's monitors. - Sets ``self.stored_filename`` to the resulting path. No-op if + Sets ``self.stored_filename`` to the resulting path; leaves + ``self.last_arrays`` itself as the plain in-memory arrays already + built by ``_build_arrays`` (unlike ``CounterValue.store_arrays``, + this does not reload/replace them with file-backed handles after + writing -- see the comment below for why not). No-op if ``self.last_arrays`` is empty (nothing to store). """ if not self.last_arrays: @@ -640,13 +644,19 @@ class BsStreamCounter: self.stored_filename = path.resolve().as_posix() print(f"{self.name}: stored filename {self.stored_filename}") - # Re-open read-back handles (same as CounterValue.store_arrays) so - # self.last_arrays keeps working after this method returns rather - # than referencing data tied to the now-closed results_file. - d = DataSet.load_from_result_file(path) - for k in names: - self.last_arrays[k] = d.datasets[k] - d.results_file.close() + # Deliberately does NOT re-open/replace self.last_arrays[k] with a + # freshly reloaded, file-backed handle the way + # CounterValue.store_arrays does for its own ArrayTimestamps + # objects: escape.Array's own dataset wrapper (ArrayH5Dataset) is + # lazy for at least some attributes even past load_from_result_file + # (default lazy_loading=False notwithstanding), so a *second* + # load-then-close round-trip here left self.last_arrays holding + # objects tied to an already-closed h5py.File - any later access + # (e.g. arr._scan_parameter) raised deep inside h5py rather than + # working, found by actually reading last_arrays back after a real + # scan. The arrays already in self.last_arrays (from _build_arrays) + # are plain in-memory numpy-backed Array objects with no file + # dependency at all, so there is nothing to refresh - keep them. def close(self): """Unsubscribe every channel for good (the one deliberate use of