Hi John and thanks for the positive feedback !
You're right, should the database connection be closed between the time when the event is emitted and the time the listener handles the event, an error will be thrown.
In the specific case of CLI commands, where the database connection is not persistent, using app methods throwing events, which is your use case, this can indeed happen. If the database connection does persist however, this situation would be extremely rare and would probably only occur for reasons that will likely break the entire application anyway.
This is solved by implementing a mechanism that basically waits for all triggered events to be successfully handled before closing the database. There are multiple ways to do this (largely out of the scope of the series), but it would revolve around the idea of throwing success events in your application-layer classes and ensuring all those events have been emitted before closing.