Skip to content

Conversation

MikeSullivan7
Copy link
Collaborator

@MikeSullivan7 MikeSullivan7 commented Aug 6, 2025

Issue Closes #2715

Description

The PR is to prevent the QThread in the Spectrum Viewer from changing data which may be accessed by different processes, while the QThread is running as this may lead to unreliable results due to a race condition. To achieve this, the function in the Worker Thread no longer directly accesses the attributes in the presenter, but instead takes them as arguments and outputs the altered attributes as thread.result(). The attributes in the presenter space are then changed during the thread_cleanup instead of inside the thread itself.

Also to make sure that nothing could be changed in the running of the thread, a Lock() is used inside the thread function.

Developer Testing

  • I have verified unit tests pass locally: python -m pytest -vs

Acceptance Criteria and Reviewer Testing

  • Unit tests pass locally: python -m pytest -vs
  • Load data in the spectrum viewer and open multiple ROIs
  • Move the position and sizes of the ROIs in any combination of ways you can think of and as fast as you feasibly can.
  • ensure that the spectrums update as expected and that no errors are raised.

@coveralls
Copy link

coveralls commented Aug 6, 2025

Coverage Status

coverage: 70.37% (+0.08%) from 70.295%
when pulling faf4a94 on 2715_spectrum_thread_mutable
into 3b7fb16 on main.

@MikeSullivan7 MikeSullivan7 marked this pull request as ready for review August 12, 2025 16:36
@MikeSullivan7 MikeSullivan7 changed the title use threading Lock() in run_spectrum_calculation Spectrum Viewer: Prevent QThread from accessing Presenter data during runtime Aug 12, 2025
@MikeSullivan7 MikeSullivan7 marked this pull request as draft August 13, 2025 17:14
@MikeSullivan7
Copy link
Collaborator Author

The kwargs for the TaskWorkerThread has now been changed such that the function receives a copy of the input data which it then modifies, instead of modifying the attributes of the presenter directly. The presenter attributes are then changed in the thread cleanup.
This can be checked via e.g.

print(f"{(self.roi_to_process_queue is self.thread.result[0])=}")
print(f"{(self.image_nan_mask_dict is self.thread.result[1])=}")
print(f"{(self.view.spectrum_widget.spectrum_data_dict["roi"] is self.thread.result[2]["roi"])=}")

In the thread_cleanup() method

The only thing which is not local in run_spectrum_calculation now is the use of self.model.get_spectrum() but as this is purely a getter, this will not modify any non-local data from inside the worker thread.

@MikeSullivan7 MikeSullivan7 marked this pull request as ready for review August 15, 2025 15:57
@samtygier-stfc
Copy link
Collaborator

Would it work to move all the chunk logic into handle_roi_moved() and for i in range(len(spectrum)): into thread_cleanup()? it is only really the self.model.get_spectrum() call that is slow an needs to be in the thread. I think that would save from needing the copies, because its only some simple values being passed in, and the spectrum array being passed back.

@samtygier-stfc samtygier-stfc self-assigned this Aug 20, 2025
@MikeSullivan7 MikeSullivan7 force-pushed the 2715_spectrum_thread_mutable branch from 20845ed to 168cac9 Compare August 21, 2025 15:58
@MikeSullivan7 MikeSullivan7 marked this pull request as draft August 28, 2025 09:07
@MikeSullivan7 MikeSullivan7 force-pushed the 2715_spectrum_thread_mutable branch from 168cac9 to 34b1b39 Compare September 1, 2025 14:56
@MikeSullivan7 MikeSullivan7 force-pushed the 2715_spectrum_thread_mutable branch from 34b1b39 to 044d627 Compare September 1, 2025 15:48
@MikeSullivan7 MikeSullivan7 marked this pull request as ready for review September 1, 2025 16:56
ashleymeigh2
ashleymeigh2 previously approved these changes Sep 2, 2025
Copy link
Collaborator

@ashleymeigh2 ashleymeigh2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@JackEAllen JackEAllen marked this pull request as draft September 2, 2025 12:20
@MikeSullivan7 MikeSullivan7 force-pushed the 2715_spectrum_thread_mutable branch from 044d627 to faf4a94 Compare September 5, 2025 14:01
@MikeSullivan7 MikeSullivan7 marked this pull request as ready for review September 5, 2025 14:01
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.

Spectrum Viewer: Refactor to prevent Qthread from accessing mutable data
4 participants