firebase-admin-python icon indicating copy to clipboard operation
firebase-admin-python copied to clipboard

FR: Some way of knowing if a listener thread has died

Open Zhipan1 opened this issue 6 years ago • 0 comments

Describe your environment

  • Operating System version: OSX 10.15.7
  • Firebase SDK version: 4.4.0
  • Firebase Product: database

Describe the problem

Event listeners run in a separate thread so if ListenerRegistration._start_listen throws an exception, the listener will die see link. The user has no way of determining if a listener has died since the exception doesn't propagate to the main app, as it's in another thread. In other words, any app that uses the listener has no way of knowing that the listener has died if ListenerRegistration._start_listen raises. To them, it will just appear as if there are no new events (rather than that the listener has died).

The listener will die if ListenerRegistration._start_listen errors and that can happen in 2 ways:

(1) if ListenerRegistration._callback errors (2) if the iterator ListenerRegistration._sse errors

For (1) the user can just wrap the callback in a try catch to prevent the error from propagating to _start_listen. There is nothing the user can do about (2).

There are many places where (2) will throw an exception. For us, this happens when our instances temporarily lose their internet connection. There is retry logic in the SSEClient but eventually it will crash on this line. The request library will throw this exception: requests.exceptions.ConnectionError: ('Connection aborted.', OSError(0, 'Error')).

Potential Solution

At the very least, it would be nice if the user has some way of knowing that the listener has died, so they can handle the issue. The way we handle it right now is to check if the thread is still alive every 5 seconds. It would be a lot nicer if there is a callback such as ListenerRegistration.on_disconnect. This can be achieved by wrapping _start_listen in a try catch and calling on_disconnect when an exception occurs, then rethrowing that exception. The code will work exactly the same as before but with the added benefit of the on_disconnect callback.

Zhipan1 avatar Oct 26 '20 18:10 Zhipan1