Skip to content

Fix race condition in MainThreadRunner with thread-indexed result storage#40

Merged
iamtekson merged 4 commits intodevfrom
copilot/sub-pr-38-again
Jan 5, 2026
Merged

Fix race condition in MainThreadRunner with thread-indexed result storage#40
iamtekson merged 4 commits intodevfrom
copilot/sub-pr-38-again

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 5, 2026

The MainThreadRunner class used shared instance variables _result and _error accessible by both the main thread and concurrent worker threads. Multiple workers calling execute_on_main_thread simultaneously could overwrite each other's results.

Changes:

  • Replaced instance variables with thread-indexed dictionary _results
  • Added threading.Lock to protect dictionary access
  • Modified run_task slot to accept and store results by thread_id
  • Modified execute_on_main_thread to retrieve results using caller's thread ID
  • Added explicit existence check before retrieval to avoid masking legitimate None returns

Before:

@pyqtSlot(object, list, dict)
def run_task(self, func, args, kwargs):
    self._result = None  # Shared by all threads
    self._error = None
    try:
        self._result = func(*args, **kwargs)
    except Exception as e:
        self._error = e

After:

@pyqtSlot(object, list, dict, int)
def run_task(self, func, args, kwargs, thread_id):
    result = None
    error = None
    try:
        result = func(*args, **kwargs)
    except Exception as e:
        error = e
    
    with self._lock:
        self._results[thread_id] = (result, error)  # Isolated per thread

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 5, 2026 05:08
Co-authored-by: iamtekson <39838116+iamtekson@users.noreply.github.com>
Co-authored-by: iamtekson <39838116+iamtekson@users.noreply.github.com>
Copilot AI changed the title [WIP] WIP address feedback from review on illegal threading issue resolution Fix race condition in MainThreadRunner with thread-indexed result storage Jan 5, 2026
Copilot AI requested a review from iamtekson January 5, 2026 05:10
@iamtekson iamtekson marked this pull request as ready for review January 5, 2026 05:15
@iamtekson iamtekson merged commit 760a63b into dev Jan 5, 2026
1 check passed
@iamtekson iamtekson deleted the copilot/sub-pr-38-again branch January 5, 2026 05:15
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.

2 participants