Title: [Bug]: Event loop behavior differs across backends · Issue #31858 · matplotlib/matplotlib · GitHub
Open Graph Title: [Bug]: Event loop behavior differs across backends · Issue #31858 · matplotlib/matplotlib
X Title: [Bug]: Event loop behavior differs across backends · Issue #31858 · matplotlib/matplotlib
Description: Bug summary I'm working on fixing several bugs with the macOS backend. I am trying to understand how show(), start_event_loop(), and stop_event_loop() should interact with each other. I had several questions that I couldn't find in the d...
Open Graph Description: Bug summary I'm working on fixing several bugs with the macOS backend. I am trying to understand how show(), start_event_loop(), and stop_event_loop() should interact with each other. I had several...
X Description: Bug summary I'm working on fixing several bugs with the macOS backend. I am trying to understand how show(), start_event_loop(), and stop_event_loop() should interact with each other. I had sev...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/31858
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[Bug]: Event loop behavior differs across backends","articleBody":"### Bug summary\n\nI'm working on fixing several bugs with the macOS backend. I am trying to understand how `show()`, `start_event_loop()`, and `stop_event_loop()` should interact with each other.\n\nI had several questions that I couldn't find in the documentation:\n\n1) If `show(block=True)` is called, should `stop_event_loop()` make it return?\n2) If `start_event_loop()` is called on a figure, should closing all windows make it return?\n3) What should be the behavior if `start_event_loop()` or `show(block=True)` is called when an event loop is already running from `show()` or another `start_event_loop()`?\n4) If `stop_event_loop()` is called without an event loop running, it appears to be a no-op across all backends. Should it instead throw an exception?\n\nI wrote the attached code to test these. However, I am seeing different results from different backends. See charts in the \"Actual Results\" section.\n\n### Code for reproduction\n\n```Python\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport sys\n\nexample_index = int(sys.argv[1])\n\nbackend, mode = (\n ( \"QtAgg\", \"show\" ),\n ( \"TkAgg\", \"show\" ),\n ( \"macosx\", \"show\" ),\n ( \"QtAgg\", \"start_event_loop\" ),\n ( \"TkAgg\", \"start_event_loop\" ),\n ( \"macosx\", \"start_event_loop\" ),\n)[ example_index ]\n\nprint(f\"backend = '{backend}', mode = '{mode}'\")\n\n\nmatplotlib.use(backend)\n\nfig, ax = plt.subplots()\nax.set_title(\"Click anywhere to continue\")\nax.plot([1, 2, 3])\n\nclicked = []\n\ndef on_click(event):\n clicked.append((event.xdata, event.ydata))\n print(\"on_click\")\n fig.canvas.stop_event_loop()\n\nfig.canvas.mpl_connect('button_press_event', on_click)\n\nfig, ax = plt.subplots()\n\nif (mode == \"show\"):\n plt.show()\nelse:\n plt.show(block=False)\n fig.canvas.start_event_loop()\n\nif (len(clicked) \u003e 0):\n print(f\"User clicked at: {clicked[0]}\")\nelse:\n print(f\"Event loop stopped without click\")\n```\n\n### Actual outcome\n\n#### Does closing both windows stop the event loop?\n\n| `backend` | `mode` | Result |\n|-|-|-|\n| QtAgg | show | Yes |\n| TkAgg | show | Yes |\n| macosx | show | Yes |\n| QtAgg | start_event_loop | No |\n| TkAgg | start_event_loop | **Yes** |\n| macosx | start_event_loop | No |\n \n\n#### Does clicking on Figure 1 stop the event loop?\n\n| `backend` | `mode` | Result |\n|-|-|-|\n| QtAgg | show | No |\n| TkAgg | show | **Yes** |\n| macosx | show | No |\n| QtAgg | start_event_loop | Yes |\n| TkAgg | start_event_loop | Yes |\n| macosx | start_event_loop | Yes |\n\n### Expected outcome\n\nAll backends should operate with the same behavior.\n\n### Additional information\n\n_No response_\n\n### Operating system\n\n_No response_\n\n### Matplotlib Version\n\nDev\n\n### Matplotlib Backend\n\n_No response_\n\n### Python version\n\n_No response_\n\n### Jupyter version\n\n_No response_\n\n### Installation\n\nNone","author":{"url":"https://github.com/iccir","@type":"Person","name":"iccir"},"datePublished":"2026-06-09T02:18:06.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/31858/matplotlib/issues/31858"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:7e48a31a-68a4-1de2-a27b-e84cf8849180 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EB5C:2180CA:64A3C3:86D551:6A53EFEB |
| html-safe-nonce | d0fd286e26c487bceba18d45edaffb10d6edfcb12155d1575c53a35dec2dceac |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjVDOjIxODBDQTo2NEEzQzM6ODZENTUxOjZBNTNFRkVCIiwidmlzaXRvcl9pZCI6Ijg4MzI4MzczNDEwODc1MjY4OTEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5bb731875aa3396d6de2dcb5fda5f90c96b31bbf423f536d7cbff81fc858a21f |
| hovercard-subject-tag | issue:4618198690 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/31858/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4068e705ef3641ecb2509fcd49c77a80e7e0f327440cb340ea03a9f7acd1dcb1/matplotlib/matplotlib/issues/31858 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4068e705ef3641ecb2509fcd49c77a80e7e0f327440cb340ea03a9f7acd1dcb1/matplotlib/matplotlib/issues/31858 |
| og:image:alt | Bug summary I'm working on fixing several bugs with the macOS backend. I am trying to understand how show(), start_event_loop(), and stop_event_loop() should interact with each other. I had several... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iccir |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width