Skip to content

[MNT] Fix Tkinter UI crashes during test suite execution via headless Matplotlib backend#2142

Open
Siddhazntx wants to merge 5 commits intosktime:mainfrom
Siddhazntx:mnt/fix-test-ui-crash
Open

[MNT] Fix Tkinter UI crashes during test suite execution via headless Matplotlib backend#2142
Siddhazntx wants to merge 5 commits intosktime:mainfrom
Siddhazntx:mnt/fix-test-ui-crash

Conversation

@Siddhazntx
Copy link

Reference Issues/PRs

Fixes #2140

What does this implement/fix? Explain your changes.

When running the test suite locally (specifically on Windows Conda environments or headless CI environments), tests that invoke model plotting (such as test_temporal_fusion_transformer.py::test_integration) cause the test suite to crash.

The crash occurs because self.plot_prediction() calls plt.subplots(). If the environment defaults to a GUI backend like TkAgg but lacks properly configured UI libraries, it attempts to open a physical window and fails.

Terminal Error Traceback
tests\test_models\test_baseline.py::test_integration[multiple_dataloaders_with_covariates0] ✓            57% █████▊   
 tests\test_models\test_temporal_fusion_transformer.py::test_integration[multiple_dataloaders_with_covariates0] ✓57% █████▊                                                                                                                        
 tests\test_models\test_baseline.py::test_integration[multiple_dataloaders_with_covariates1] ✓            57% █████▊   
 tests\test_models\test_temporal_fusion_transformer.py::test_integration[multiple_dataloaders_with_covariates1] ✓58% █████▊                                                                                                                        
 tests\test_models\test_baseline.py::test_integration[multiple_dataloaders_with_covariates2] ✓            58% █████▊   
 tests\test_models\test_temporal_fusion_transformer.py::test_integration[multiple_dataloaders_with_covariates2] ✓58% █████▊                                                                                                                        
 tests\test_models\test_baseline.py::test_integration[multiple_dataloaders_with_covariates3] ✓            58% █████▉   
 tests\test_models\test_temporal_fusion_transformer.py::test_integration[multiple_dataloaders_with_covariates3] ✓59% █████▉   
...
=============================================== short test summary info =============================================== 
SKIPPED [1] tests\test_metrics.py:446: CUDA is not available
SKIPPED [1] tests\test_models\test_temporal_fusion_transformer.py:274: Test skipped on Windows OS due to issues with ddp, see #1623
SKIPPED [2] tests\test_models\test_temporal_fusion_transformer.py:432: Test skipped on Win due to bug #1632, or if missing required packages
SKIPPED [1] tests\test_models\test_timexer.py:501: Skipping due to incompatibility with current model outputs.

Results (282.77s (0:04:42)):
     387 passed
       5 skipped

The Fix:

This PR updates tests/conftest.py to explicitly set matplotlib.use("Agg") at the very top of the file. This globally forces Matplotlib to use the headless Agg backend during pytest execution. Plots are now safely rendered in memory without attempting to launch physical GUI windows.

What should a reviewer concentrate their feedback on?

The placement of matplotlib.use("Agg") in conftest.py. It is placed at the top of the file to ensure it executes before any pytorch_forecasting modules are imported, which might otherwise prematurely initialize pyplot with the default backend.

PR checklist

  • The PR title starts with either [ENH], [MNT], [DOC], or [BUG]. [BUG] - bugfix, [MNT] - CI, test framework, [ENH] - adding or improving code, [DOC] - writing or improving documentation or docstrings.
  • Added/modified tests
  • Used pre-commit hooks when committing to ensure that code is compliant with hooks. Install hooks with pre-commit install.
    To run hooks independent of commit, execute pre-commit run --all-files

@Siddhazntx Siddhazntx force-pushed the mnt/fix-test-ui-crash branch from 0eabe9b to 41807c2 Compare March 4, 2026 20:04
@codecov
Copy link

codecov bot commented Mar 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@602ee15). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2142   +/-   ##
=======================================
  Coverage        ?   86.63%           
=======================================
  Files           ?      165           
  Lines           ?     9732           
  Branches        ?        0           
=======================================
  Hits            ?     8431           
  Misses          ?     1301           
  Partials        ?        0           
Flag Coverage Δ
cpu 86.63% <100.00%> (?)
pytest 86.63% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

[BUG] : Test suite crashes on Windows/headless environments due to Matplotlib Tkinter backend

1 participant