Fix flaky test_hist_file_config by Carreau · Pull Request #15169 · ipython/ipython

@google-labs-jules @Carreau

Explicitly stop HistorySavingThread in test_hist_file_config to ensure
the HistoryManager instance is ready for garbage collection during
teardown. Also fix a typo in resetting _max_inst.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>

bot and others added 12 commits

March 26, 2026 16:53
- Explicitly stop HistorySavingThread and close database connections in
  tests/test_history.py to ensure HistoryManager instances can be
  garbage collected and files are unlocked.
- Robustly restore ip.history_manager in tests.
- Fix typo and logic for resetting HistoryManager._max_inst.
- Addresses issue #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Explicitly stop HistorySavingThread and close database connections in
  tests/test_history.py to ensure HistoryManager instances can be
  garbage collected and files are unlocked.
- Robustly restore ip.history_manager in tests.
- Fix typo and logic for resetting HistoryManager._max_inst.
- Clean up test_history and other tests to avoid potential leaks and
  ensure proper state restoration.
- Addresses issue #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Explicitly stop HistorySavingThread and close database connections in
  tests/test_history.py and tests/test_pylabtools.py to ensure
  HistoryManager instances can be garbage collected and files are
  unlocked on Windows.
- Robustly restore ip.history_manager in tests.
- Fix typo and logic for resetting HistoryManager._max_inst, ensuring it
  matches platform-specific expectations.
- Use .copy() and .update() for matplotlib.rcParams to preserve its type
  and avoid AttributeErrors in subsequent tests.
- Addresses issue #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests to
  allow garbage collection and prevent 'assert 2 == 1' failures.
- Fix regression in tests/test_pylabtools.py where matplotlib.rcParams
  was being overwritten with a dict instead of being updated, breaking
  Matplotlib internal logic.
- Properly close HistoryManager in shell_pylab_fixture.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests to
  allow garbage collection and prevent 'assert 2 == 1' failures.
- Fix regression in tests/test_pylabtools.py where matplotlib.rcParams
  was being overwritten with a dict instead of being updated, breaking
  Matplotlib internal logic.
- Properly close HistoryManager in shell_pylab_fixture.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests to
  allow garbage collection and prevent 'assert 2 == 1' failures.
- Fix regression in tests/test_pylabtools.py where matplotlib.rcParams
  was being overwritten with a dict instead of being updated, breaking
  Matplotlib internal logic.
- Properly close HistoryManager in shell_pylab_fixture.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Remove experimental test_calling_run_cell to avoid side effects.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests to
  allow garbage collection and prevent 'assert 2 == 1' failures.
- Fix regression in tests/test_pylabtools.py where matplotlib.rcParams
  was being overwritten with a dict instead of being updated, breaking
  Matplotlib internal logic.
- Properly close HistoryManager in shell_pylab_fixture.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Remove experimental test_calling_run_cell to avoid side effects.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests to
  allow garbage collection and prevent 'assert 2 == 1' failures.
- Fix regression in tests/test_pylabtools.py where matplotlib.rcParams
  was being overwritten with a dict instead of being updated, breaking
  Matplotlib internal logic.
- Properly close HistoryManager in shell_pylab_fixture.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Remove experimental test_calling_run_cell to avoid side effects.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests to
  allow garbage collection and prevent 'assert 2 == 1' failures.
- Fix regression in tests/test_pylabtools.py where matplotlib.rcParams
  was being overwritten with a dict instead of being updated, breaking
  Matplotlib internal logic.
- Properly close HistoryManager in shell_pylab_fixture.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests to
  allow garbage collection and prevent 'assert 2 == 1' failures.
- Fix regression in tests/test_pylabtools.py where matplotlib.rcParams
  was being overwritten with a dict instead of being updated, breaking
  Matplotlib internal logic.
- Properly close HistoryManager in shell_pylab_fixture.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests to
  allow garbage collection and prevent 'assert 2 == 1' failures.
- Fix regression in tests/test_pylabtools.py where matplotlib.rcParams
  was being overwritten with a dict instead of being updated, breaking
  Matplotlib internal logic.
- Properly close HistoryManager in shell_pylab_fixture.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests to
  allow garbage collection and prevent 'assert 2 == 1' failures.
- Fix regression in tests/test_pylabtools.py where matplotlib.rcParams
  was being overwritten with a dict instead of being updated, breaking
  Matplotlib internal logic.
- Properly close HistoryManager in shell_pylab_fixture.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Remove experimental test_calling_run_cell to avoid side effects.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests by
  stopping the background saving thread and closing the database connection.
- This prevents 'AssertionError: assert 2 == 1' caused by leaked instances
  held by background threads and avoids file locking issues on Windows.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Add null checks before stopping threads or closing databases for safety.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
- Ensure HistoryManager instances are explicitly shut down in tests by
  stopping the background saving thread and closing the database connection.
- This prevents 'AssertionError: assert 2 == 1' caused by leaked instances
  held by background threads and avoids file locking issues on Windows.
- Correct typo from __max_inst to _max_inst in test_hist_file_config.
- Use local variables and null-checks to ensure safe cleanup in tests.
- Fixes #15161.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
This commit addresses the flaky `AssertionError: assert 2 == 1` in
`tests/test_history.py` (issue #15161). The root cause was the
`HistorySavingThread` holding a strong reference to the `HistoryManager`
instance during its initialization and operation, which delayed garbage
collection and caused subsequent tests to fail the instance count check.

Changes:
- Added `try...finally` blocks in `tests/test_history.py` to explicitly
  stop the `save_thread` and close the database connection for every
  `HistoryManager` instance created.
- Fixed a regression in `tests/test_pylabtools.py` where `matplotlib.rcParams`
  was being replaced by a `dict` instead of an `RcParams` object.
- Corrected a typo in `HistoryManager._max_inst` (was `__max_inst`).
- Ensured `HistoryManager._max_inst` is only reset on non-Windows
  platforms to match `conftest.py` logic.
- Improved resource cleanup in `tests/test_pylabtools.py` to prevent
  database file locking on Windows.

These changes ensure robust test isolation and prevent resource leaks
that lead to CI instability.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
This commit updates the previous fix for flaky HistoryManager tests.
It reverts the matplotlib.rcParams management in tests/test_pylabtools.py
to use plain dictionary assignment, as the previous attempt with
.update() may have caused issues in the CI environment.

Key changes:
- Restored original matplotlib.rcParams mocking style in tests/test_pylabtools.py
  while keeping the essential HistoryManager resource cleanup.
- Removed redundant manual reset of HistoryManager._max_inst in
  tests/test_history.py, relying on the hmmax fixtures instead.
- Maintained robust cleanup blocks for all HistoryManager instances to
  prevent reference leaks and file locking issues.

These changes ensure the tests are stable while adhering to the
repository's established patterns.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
This commit addresses the flaky `AssertionError: assert 2 == 1` in
`tests/test_history.py` (issue #15161). The root cause was the
`HistorySavingThread` holding a strong reference to the `HistoryManager`
instance during its initialization and operation, which delayed garbage
collection and caused subsequent tests to fail the instance count check.

Changes:
- Added `try...finally` blocks in `tests/test_history.py` to explicitly
  stop the `save_thread` and close the database connection for every
  `HistoryManager` instance created.
- Fixed a similar leak in `tests/test_pylabtools.py` by cleaning up the
  `HistoryManager` associated with the test shell.
- Removed redundant and incorrect `HistoryManager.__max_inst = 1`
  assignments.
- Improved resource cleanup consistency across the test suite.

These changes ensure robust test isolation and prevent resource leaks
that lead to CI instability and file locking issues on Windows.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
This commit addresses the flaky `AssertionError: assert 2 == 1` in
`tests/test_history.py` (issue #15161). The root cause was the
`HistorySavingThread` holding a strong reference to the `HistoryManager`
instance during its initialization and operation, which delayed garbage
collection and caused subsequent tests to fail the instance count check.

Changes:
- Added `try...finally` blocks in `tests/test_history.py` to explicitly
  stop the `save_thread` and close the database connection for every
  `HistoryManager` instance created.
- Added similar cleanup in `tests/test_pylabtools.py` for the history
  manager associated with the test shell.
- Fixed a typo in `HistoryManager._max_inst` (was `__max_inst`).
- Restored `test_calling_run_cell` with proper cleanup logic.

These changes ensure robust test isolation and prevent resource leaks
that lead to CI instability and file locking issues on Windows.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
This commit addresses the flaky `AssertionError: assert 2 == 1` in
`tests/test_history.py` (issue #15161). The root cause was the
`HistorySavingThread` holding a strong reference to the `HistoryManager`
instance during its initialization and operation, which delayed garbage
collection and caused subsequent tests to fail the instance count check.

Changes:
- Added `try...finally` blocks in `tests/test_history.py` to explicitly
  stop the `save_thread` and close the database connection for every
  `HistoryManager` instance created.
- Added similar cleanup in `tests/test_pylabtools.py` for the history
  manager associated with the test shell.
- Fixed a typo where `HistoryManager._max_inst` was referred to as
  `__max_inst`.

These changes ensure robust test isolation and prevent resource leaks
that lead to CI instability and file locking issues on Windows.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>
This commit addresses the flaky `AssertionError: assert 2 == 1` in
`tests/test_history.py` (issue #15161). The root cause was the
`HistorySavingThread` holding a strong reference to the `HistoryManager`
instance during its initialization and operation, which delayed garbage
collection and caused subsequent tests to fail the instance count check.

Changes:
- Added `try...finally` blocks in `tests/test_history.py` to explicitly
  stop the `save_thread` and close the database connection for every
  `HistoryManager` instance created.
- Added similar cleanup in `tests/test_pylabtools.py` for the history
  manager associated with the test shell.
- Removed redundant and incorrect `HistoryManager.__max_inst = 1`
  assignments.

These changes ensure robust test isolation and prevent resource leaks
that lead to CI instability and file locking issues on Windows.

Co-authored-by: Carreau <335567+Carreau@users.noreply.github.com>