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

make RustNotify a context manager and kill the thread on exit #164

Merged
merged 5 commits into from
Jun 25, 2022

Conversation

samuelcolvin
Copy link
Owner

fix #163

@graingert this is what I meant. Could you test and check that this is closing the thread on exit?

@codecov
Copy link

codecov bot commented Jun 22, 2022

Codecov Report

Merging #164 (fee5863) into main (93f744d) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #164   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          375       375           
  Branches        79        79           
=========================================
  Hits           375       375           
Impacted Files Coverage Δ
watchfiles/main.py 100.00% <100.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 93f744d...fee5863. Read the comment docs.

}

pub fn close(&mut self) {
self.watcher = WatcherEnum::None;
Copy link

@graingert graingert Jun 23, 2022

Choose a reason for hiding this comment

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

Can you override __del__ to issue a ResourceWarning if the watcher is not None?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Maybe, but I'm not sure it's worth it - if you actually delete the instance the thread will be closed. So technically __del__ is just as correct as __exit__ or close.

Also RustNotify is not supposed to be used directly and both the public methods which use take care of clean up.

I think what we have here is sufficient.

Choose a reason for hiding this comment

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

  • if you actually delete the instance the thread will be closed.

Yes this is the purpose of ResourceWarning in a __del__ method

Choose a reason for hiding this comment

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

pytest hooks into this GC/ResourceWarning system to fail the test suite if an explicit close is forgotten

Copy link
Owner Author

Choose a reason for hiding this comment

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

But omitting to call close is fine in general.

If you'd like to submit a PR, feel free.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Apart from your warning suggestion on __del__, can you confirm if this works?

Choose a reason for hiding this comment

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

yep this fixes it!

/tmp/tmphp91terl
watcher: INotifyWatcher { channel: Sender { .. }, waker: Waker { inner: Waker { fd: File { fd: 5, path: "anon_inode:[eventfd]", read: true, write: true } } } }
keyboard interrupt happened gen=<generator object watch at 0x7f2abdb68a50> is closed gen.gi_frame=None is unset but still we're listening

Copy link
Owner Author

Choose a reason for hiding this comment

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

On reflection I think you're right that we should raise a resource warning, fuller answer below.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I've created #166 to keep track of adding a resource warning. I think htis can be merged through since we shouldn't wait for pyo3 to resolve the original bug here.

@samuelcolvin samuelcolvin merged commit 6349679 into main Jun 25, 2022
@samuelcolvin samuelcolvin deleted the RustNotify-context-manager branch June 25, 2022 13:00
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.

stopping daemon watcher thread when watching is finished
2 participants