Skip to content

MATLAB hybrid reference test loses the tutorial figure target between calls #580

Closed

Description

@suraj-ranganath

Context

tests/test_eeglab_hybrid.py::test_matlab_tutorial_figures_retain_their_plot_target_between_calls fails in the MATLAB batch reference environment on develop independently of the stream PRs.

Evidence was read from [email protected] (jamming), under /home/suraj3/eegprep-bench/logs/:

Run Evidence Observed parent / requested figure
Develop, 1bdd1a28353a4dfd51ffacd7b327443836de7f29 s11_ref/rerun_hybrid_develop.log 1 / 2; 1 failed in 5.11 s
S11, 8a02dd79a23cd1cb62dc4d9e84b2df94a94620fb, isolated s11_ref/rerun_hybrid_tip.log 1 / 2; 1 failed in 5.11 s
S9, 74519da7804f9b6dd5e2923918e805fcc4d782d4, full hybrid shard s9_ref/hybrid.pytest.log, s9_ref/junit_hybrid.xml 1 / 2
S11, full hybrid shard s11_ref/hybrid.pytest.log, s11_ref/junit_hybrid.xml 2 / 4

s11_ref/loose_ends3.out:109 records the develop SHA for the isolated run. Both isolated reruns used fresh batch servers, as shown by s11_ref/run_s11_loose_ends3.sh. The launcher used an R2025b preference directory with DISPLAY, WAYLAND_DISPLAY, and XAUTHORITY unset.

The S5 hybrid log at 585a4463d7afa45e4d6434336d2f714a33024a04 records this test passing (s5_ref/hybrid.pytest.log:149). The isolated 1 / 2 failure repeats in the supplied reruns; the broader evidence also shows sensitivity to execution context and figure state.

Reference pins: EEGLAB d0963be9d81acc5b66e84f8d3d7c89e3ce127cc4, eeglab_tests fec88f9ffe7c225eb1de8924db8b6a3e0d53a4d9. Local source inspection used 64e8d2fe6eb903945407c00f11f610a7552fe42c; the failing test and tutorial-figure helper are unchanged from 1bdd1a28. No MATLAB session was started during this investigation.

Repro steps

  1. Use develop and the pinned reference checkouts. Match the recorded headless batch environment, with EEGPREP_MATLAB_BATCH_DIR pointing to its configured server directory.
  2. For a future reproduction on jamming, hold flock ~/eegprep-bench/matlab.lock for the entire MATLAB session and test execution, with one MATLAB session at a time. The existing run_s11_loose_ends3.sh documents the original environment and server setup.
  3. In that configured environment, run the isolated command recorded by the script:
uv run --no-sync pytest -vv \
  --eeglab-backend=matlab \
  --eeglab-root="$HOME/eegprep-bench/eeglab" \
  --eeglab-suite-root="$HOME/eegprep-bench/eeglab_tests" \
  --timeout=1200 --timeout-method=signal -p no:cacheprovider \
  tests/test_eeglab_hybrid.py::test_matlab_tutorial_figures_retain_their_plot_target_between_calls

The test temporarily sets DefaultFigureVisible to on. For each of four figures, it sends separate requests to create a figure, plot two points, retrieve gca, and retrieve that axes object's Parent. It asserts equality with the newly created figure handle, then closes figures and restores visibility in finally.

Observed

The isolated develop log reports eeglab_matlab_engine = <tests.matlab_batch_engine.BatchEngine ...> and fails at tests/test_eeglab_hybrid.py:683:

> np.testing.assert_array_equal(parent, figure)
E AssertionError:
E Arrays are not equal
E Mismatched elements: 1 / 1 (100%)
E Max absolute difference among violations: 1.
E Max relative difference among violations: 0.5
E  ACTUAL: array([[1.]])
E  DESIRED: array([[2.]])

The requested figure and subsequently inspected current axes disagree. The assertion alone cannot establish whether plot used the wrong figure or whether the current figure changed between plotting and the later gca request.

Expected

Across the separate calls, each new tutorial figure retains its intended plotting target. Its plotted axes have that figure as their parent, and gca refers to those axes. All four comparisons pass exactly, while cleanup restores the original figure visibility.

Likely root cause

The reference harness relies on implicit MATLAB graphics state surviving independent requests. Delayed figure activation or graphics callbacks can change groot.CurrentFigure after the helper returns. The headless batch server processes intervening waits, and later plot and gca calls have no explicit target.

Relevant code paths, with line numbers at 64e8d2fe:

  • tests/test_eeglab_hybrid.py:673-686: figure creation, plotting, current-axes query, parent assertion, and cleanup.
  • tests/eeglab_tests/backend.py:179-202 -> tests/matlab/eegprep_test_call.m:1-14: each call_matlab is a separate transport request that invokes the requested MATLAB function.
  • tests/matlab/eegprep_test_tutorial_figure.m:3-6: calls figure, drawnow, and set(groot, 'CurrentFigure', figure_handle) before returning the numeric handle. This only establishes the current figure at that instant.
  • tests/matlab/eegprep_test_gui_handle.m:3: returns double(feval(function_name, varargin{:})), so the gca request reads whichever figure is current then.
  • tests/matlab/eegprep_batch_engine_serve.m:11-24 and tests/matlab_batch_engine.py:186-191: server pause(0.01) and client request waits separate the graphics operations.
  • tests/test_tutorial_eeglab_tests.py:106-110: tutorial reference cases also use this MATLAB figure helper.

This points to the MATLAB reference harness and graphics lifecycle. The failing path invokes MATLAB figure, plot, gca, and get through EEGPrep's test helpers; no EEGPrep Qt dialog is involved. Graphics callbacks are a hypothesis from source and logs. Instrument CurrentFigure before and after each request and inspect the actual plotted line's axes/figure ancestry to establish exactly when the target changes. Compare visible and hidden figures and batch versus native Engine execution when a reference session is available.

Definition of done

  • Identify the precise state transition that changes the intended figure or current axes, with evidence from the recorded headless batch configuration.
  • Repair the helper or transport state handling so tutorial plotting retains its target across separate calls. Keep the exact handle assertion and the visible-figure scenario.
  • Pass the exact isolated node repeatedly from fresh sessions, then pass it in the complete hybrid shard and relevant tutorial reference tests.
  • Preserve test_matlab_reference_figures_stay_off_desktop, restore DefaultFigureVisible, and leave no figures behind after success or failure.
  • Record MATLAB version, transport, display configuration, reference pins, and test results. Preserve all numerical tolerances.

Activity

  1. claude commented on Nov 8, 2026

    @claude
    Contributor

    🤖 Plan:

    • Read issue, harness code, duplicate preflight (no open PRs/issues overlap)
    • Root-cause analysis from source
    • Reproduce + instrument in the recorded headless R2025b batch environment (needs human with MATLAB; not available on this runner)
    • Implement fix and run isolated node repeatedly, hybrid shard, tutorial tests

    No PR: the definition of done requires MATLAB evidence from the jamming batch environment, which this runner cannot produce. Analysis for a human below.

    Research

    In the isolated run the first figure created by the test is number 2, so figure 1 was still alive when eegprep_test_tutorial_figure ran. The only figure created earlier in that fresh session is the figure_visibility probe the test itself calls first, which closes its figure via onCleanup after varargout is assigned. In every recorded failure the plot lands on an older figure (1/2 isolated, 2/4 in the S11 shard), i.e. the axes ancestry follows a lingering earlier figure, not the requested one. Hypothesis: under headless R2025b (no DISPLAY), the probe's close is not finalized (or a deferred activation of figure 1 fires) after the helper's set(groot,'CurrentFigure',h), and the next untargeted plot/gca request picks figure 1.

    Relevant code

    Proposed Fix

    1. Confirm with instrumentation (one throwaway run): after each request log double(groot.CurrentFigure), double(findall(groot,'Type','figure'))' and, after plot, ancestor(h,'figure') of the returned line. Run once with the probe call removed to test the lingering-figure-1 hypothesis directly.
    2. If confirmed, make the probe deterministic: delete(figure_handle); drawnow; explicitly before returning instead of relying on onCleanup, so no stale figure survives into the next request.
    3. Make the helper pin the target fully, not just at one instant: create the axes and make both current, e.g.
    figure_handle = figure(varargin{:});
    axes_handle = axes('Parent', figure_handle);
    drawnow;
    set(groot, 'CurrentFigure', figure_handle);
    set(figure_handle, 'CurrentAxes', axes_handle);

    Keep the exact parent == figure assertion and the DefaultFigureVisible='on' scenario; close all force plus the visibility restore in finally already cover cleanup.

    Open question: if instrumentation shows CurrentFigure flipping during the server's pause (deferred activation rather than a lingering figure), step 3 alone may not suffice and the batch server should drawnow after each request before writing the response.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions