Skip to content

BackgroundConsumer destruction issues #788

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

Closed
daniel-sanche opened this issue Feb 1, 2025 · 3 comments · Fixed by #814
Closed

BackgroundConsumer destruction issues #788

daniel-sanche opened this issue Feb 1, 2025 · 3 comments · Fixed by #814
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@daniel-sanche
Copy link
Contributor

daniel-sanche commented Feb 1, 2025

We recently merged a PR that clears some variables that were causing a memory leak in the bidirectional streaming classes. It seems like after getting that fix in, it exposed a deeper issue: the background thread isn't exiting as expected.

Quick Fix

When this happens, it's leading to a null pointer exception here, now that the _on_response can be empty. So as a quick fix, we should add a None check there

Deeper Issue

The root issue is that the BackgroundConsumer's background thread is getting stuck calling _on_response while being torn down. In the case of Firestore, it looks like the deadlock is the fault of the Watch class, which tries to re-enter a lock. But I think we should consider changing the BackgroundConsumer to close quicker, without doing that final on_response call at all. Is it really necessary to call that callback while being closed?

More Context

googleapis/python-firestore#985 (comment)

Let me know if you want me to submit a PR

@daniel-sanche daniel-sanche added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. priority: p2 Moderately-important priority. Fix may not be included in next release. and removed priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. labels Feb 1, 2025
@parthea
Copy link
Collaborator

parthea commented Feb 13, 2025

Yes, please go ahead and open a PR

@vchudnov-g
Copy link
Contributor

@daniel-sanche Could you file a GitHub issue for the "Deeper Issue", so we remember to come back to it?

@daniel-sanche
Copy link
Contributor Author

Actually looking at it again, I think most of the blame falls on firestore's callback implementation.

I think this should be safe to close with the fixes in place

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants