<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/how-to-stop-background-threads-on-exit-in-pyqt6-closeevent-cleanup-d72mh5tpc" -->

---
title: How to stop background threads on exit in PyQt6 —...
description: A practical guide to cleanly shutting down PyQt6 applications that use QThreadPool or QThread workers. Explains overriding closeEvent to signal background...
canonical: https://daily.dev/posts/how-to-stop-background-threads-on-exit-in-pyqt6-closeevent-cleanup-d72mh5tpc
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: How to stop background threads on exit in PyQt6 — closeEvent cleanup | daily.dev
og:description: A practical guide to cleanly shutting down PyQt6 applications that use QThreadPool or QThread workers. Explains overriding closeEvent to signal background...
og:url: https://daily.dev/posts/how-to-stop-background-threads-on-exit-in-pyqt6-closeevent-cleanup-d72mh5tpc
og:image: https://api.daily.dev/og/posts/d72mh5TPC.png
og:image:alt: How to stop background threads on exit in PyQt6 — closeEvent cleanup
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# How to stop background threads on exit in PyQt6 — closeEvent cleanup

**[Planet Python](https://daily.dev/sources/planetpython)** · 8 min read · 0 upvotes · 0 comments

## Summary

A practical guide to cleanly shutting down PyQt6 applications that use QThreadPool or QThread workers. Explains overriding closeEvent to signal background workers to stop via a running flag, then calling threadpool.waitForDone() before allowing the window to close, preventing orphaned threads and console errors. Covers managing multiple worker groups and ensures proper process exit by wrapping app.exec() in sys.exit(). Includes complete, runnable code examples.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.pythonguis.com/faq/clean-up-on-exit-application>

## Questions this post answers

### How do I stop QThreadPool worker threads when closing a PyQt6 window?

Override closeEvent on your QMainWindow, call stop() on each tracked worker to flip an is_running flag checked inside the worker's run loop, then call self.threadpool.waitForDone() to block until every runnable finishes before calling event.accept() to let the window close.

_Developers wiring up PyQt6 cleanup logic can find similar threading fixes curated on daily.dev._

### Why does my PyQt6 application not exit cleanly even after the window closes?

This usually happens when sys.exit() isn't wrapping app.exec()'s return code, or when threads are still referenced after the window closes. Always launch with sys.exit(app.exec()) so Python receives the proper exit code and triggers a SystemExit that lets it clean up resources, especially when background threads are involved.

_Anyone debugging hung Python GUI processes can track PyQt6 troubleshooting tips on daily.dev._

### How do I stop a QRunnable that doesn't have a simple loop but runs a long series of processing steps?

Add a maybe_stop() method that checks the running flag and raises an exception if it's False, then call maybe_stop() between each processing step in run(). The runnable can only actually exit at the points where the flag is checked, so more frequent checks give more responsive shutdown.

_Developers designing responsive worker cancellation in PyQt6 can follow patterns like this on daily.dev._

## Similar posts on daily.dev

- [Fix QImage NumPy Crash in PyQt6 PySide6 Multithreaded Applications](https://daily.dev/posts/fix-qimage-numpy-crash-in-pyqt6-pyside6-multithreaded-applications-w2seqhocw) · Planet Python · 0 upvotes · 0 comments
- [Wayland Wars, a Custom File Picker, and Non-Blocking Threads: Building a Responsive Qt App](https://daily.dev/posts/wayland-wars-a-custom-file-picker-and-non-blocking-threads-building-a-responsive-qt-app-pcji3yhzg) · Medium · 0 upvotes · 0 comments
- [How to Pass Arguments to QRunnable Worker Threads in PyQt6](https://daily.dev/posts/how-to-pass-arguments-to-qrunnable-worker-threads-in-pyqt6-3teggi4li) · Planet Python · 0 upvotes · 0 comments
- [Gracefully stop a child process before a macOS app quits](https://daily.dev/posts/gracefully-stop-a-child-process-before-a-macos-app-quits-g3vsnbgtr) · Flavio Copes · 0 upvotes · 0 comments
- [Qt for Python release: 6.11 is out\!](https://daily.dev/posts/qt-for-python-release-6-11-is-out--1m7bri1ie) · Qt · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#qt](https://daily.dev/tags/qt)

[View this post on daily.dev](https://daily.dev/posts/how-to-stop-background-threads-on-exit-in-pyqt6-closeevent-cleanup-d72mh5tpc)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"How to stop background threads on exit in PyQt6 — closeEvent cleanup","url":"https://daily.dev/posts/how-to-stop-background-threads-on-exit-in-pyqt6-closeevent-cleanup-d72mh5tpc","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/how-to-stop-background-threads-on-exit-in-pyqt6-closeevent-cleanup-d72mh5tpc"},"datePublished":"2026-09-09T07:51:34.863Z","dateModified":"2026-10-15T02:22:44.177Z","description":"A practical guide to cleanly shutting down PyQt6 applications that use QThreadPool or QThread workers. Explains overriding closeEvent to signal background...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/14bb6cf5f2ed4e1f76ac03317753bcd5?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/14bb6cf5f2ed4e1f76ac03317753bcd5?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet Python","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet Python","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/eac3cc584cb043b5a242d13752bd3d0c","url":"https://daily.dev/sources/planetpython"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/how-to-stop-background-threads-on-exit-in-pyqt6-closeevent-cleanup-d72mh5tpc","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,qt","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet Python","item":"https://daily.dev/sources/planetpython"},{"@type":"ListItem","position":3,"name":"How to stop background threads on exit in PyQt6 — closeEvent cleanup"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/how-to-stop-background-threads-on-exit-in-pyqt6-closeevent-cleanup-d72mh5tpc#faq","mainEntity":[{"@type":"Question","name":"How do I stop QThreadPool worker threads when closing a PyQt6 window?","acceptedAnswer":{"@type":"Answer","text":"Override closeEvent on your QMainWindow, call stop() on each tracked worker to flip an is_running flag checked inside the worker's run loop, then call self.threadpool.waitForDone() to block until every runnable finishes before calling event.accept() to let the window close. Developers wiring up PyQt6 cleanup logic can find similar threading fixes curated on daily.dev."}},{"@type":"Question","name":"Why does my PyQt6 application not exit cleanly even after the window closes?","acceptedAnswer":{"@type":"Answer","text":"This usually happens when sys.exit() isn't wrapping app.exec()'s return code, or when threads are still referenced after the window closes. Always launch with sys.exit(app.exec()) so Python receives the proper exit code and triggers a SystemExit that lets it clean up resources, especially when background threads are involved. Anyone debugging hung Python GUI processes can track PyQt6 troubleshooting tips on daily.dev."}},{"@type":"Question","name":"How do I stop a QRunnable that doesn't have a simple loop but runs a long series of processing steps?","acceptedAnswer":{"@type":"Answer","text":"Add a maybe_stop() method that checks the running flag and raises an exception if it's False, then call maybe_stop() between each processing step in run(). The runnable can only actually exit at the points where the flag is checked, so more frequent checks give more responsive shutdown. Developers designing responsive worker cancellation in PyQt6 can follow patterns like this on daily.dev."}}]}
```

