Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(script): output timing inconsistencies #2667

Merged
merged 3 commits into from
Apr 3, 2022

Conversation

indev29
Copy link
Contributor

@indev29 indev29 commented Apr 1, 2022

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update
  • Other: Replace this with a description of the type of this PR

Description

This PR fixes timing inconsistencies of script module output, first described at #2630.
Module now receives updates from corresponding runner callback, instead of directly calling get_* methods of the runner.

Synchronization had to be moved to the module itself. I don't quite like the way I did it here as it still leaves a possibility for inconsistencies to occur (e.g. if runner calls update handler during get_output execution).
The alternative would be to hold lock for the whole execution of get_output. It heavily relies on (only) module::get_output calling get_format, since explicit locking inside get_format would lead to deadlock. Although this implementation would eliminate any inconsistencies, I find it unstable architecture-wise. Maybe I've missed some other obvious solutions here.

Related Issues & Documents

Closes #2650

Documentation (check all applicable)

  • This PR requires changes to the Wiki documentation (describe the changes)
  • This PR requires changes to the documentation inside the git repo (please add them to the PR).
  • Does not require documentation changes

@codecov
Copy link

codecov bot commented Apr 1, 2022

Codecov Report

Merging #2667 (558e739) into hotfix/3.6.2 (abfc6b7) will decrease coverage by 0.00%.
The diff coverage is 0.00%.

@@               Coverage Diff                @@
##           hotfix/3.6.2    #2667      +/-   ##
================================================
- Coverage         13.78%   13.77%   -0.01%     
================================================
  Files               153      153              
  Lines             11274    11282       +8     
================================================
  Hits               1554     1554              
- Misses             9720     9728       +8     
Flag Coverage Δ
unittests 13.77% <0.00%> (-0.01%) ⬇️

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

Impacted Files Coverage Δ
src/adapters/script_runner.cpp 0.00% <0.00%> (ø)
src/modules/script.cpp 0.00% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update abfc6b7...558e739. Read the comment docs.

Copy link
Member

@patrick96 patrick96 left a comment

Choose a reason for hiding this comment

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

Synchronization had to be moved to the module itself. I don't quite like the way I did it here as it still leaves a possibility for inconsistencies to occur (e.g. if runner calls update handler during get_output execution).

Yes, that's definitely a problem.

The alternative would be to hold lock for the whole execution of get_output. It heavily relies on (only) module::get_output calling get_format, since explicit locking inside get_format would lead to deadlock. Although this implementation would eliminate any inconsistencies, I find it unstable architecture-wise. Maybe I've missed some other obvious solutions here.

I also don't think locking the whole get_output is a good idea here. However, many other modules also rely on module::get_output calling get_format and build, but I agree that that's not very clean.

I think a good way to tackle this is to copy the data struct into a local variable in the beginning of get_output and only use that struct. This resolves all the possible inconsistencies within get_output, but not the one with get_format.

For get_format, we could create a private member variable m_exit_code that we set at the beginning of get_output. This way get_format always relies on the same base-data as get_output.

This would also remove the need for individual get_* methods for all struct members.

The xworkspaces module is an example of another module that sets a member variable in get_output that is later used in a method called by module::get_output (in this case, the build method).

std::atomic_int m_pid{-1};
std::atomic_int m_exit_status{0};
data m_data;
bool m_stopping{false};
Copy link
Member

Choose a reason for hiding this comment

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

This still needs to be atomic because the stop function is called from a different thread.

@indev29
Copy link
Contributor Author

indev29 commented Apr 3, 2022

For get_format, we could create a private member variable m_exit_code that we set at the beginning of get_output. This way get_format always relies on the same base-data as get_output.

Do you mean something like this? I guess we rely on module::get_output and don't make it atomic?

Copy link
Member

@patrick96 patrick96 left a comment

Choose a reason for hiding this comment

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

Yes, that's exactly what I had in mind :)

I guess we rely on module::get_output and don't make it atomic?

Yes, the only thing that doesn't run in the main thread in the script module is handle_runner_update and there we properly lock everything.

@patrick96 patrick96 merged commit 41d41ca into polybar:hotfix/3.6.2 Apr 3, 2022
@patrick96 patrick96 added this to the 3.6.2 milestone Apr 3, 2022
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