@@ -77,19 +77,25 @@ void transactionFinalizerHandler(const TransactionCallbackPayload *payload) {
7777 * This function triggers an async invocation to call watch callbacks,
7878 * avoiding holding SQLite up.
7979 */
80- invoker->invokeAsync ([payload] {
80+
81+ // Make a copy of the payload data, this avoids a potential race condition
82+ // where the async invocation might occur after closing a connection
83+ auto dbName = std::make_shared<std::string>(*payload->dbName );
84+ int event = payload->event ;
85+ invoker->invokeAsync ([dbName, event] {
8186 try {
82- // Prevent trying to create a JSI string for a potentially closed DB
83- if (payload == NULL || payload->dbName == NULL ) {
87+
88+ ConnectionPool* connection = getConnection (*dbName);
89+ if (connection == nullptr || connection->isClosed ) {
8490 return ;
8591 }
86-
92+
8793 auto global = runtime->global ();
8894 jsi::Function handlerFunction = global.getPropertyAsFunction (
8995 *runtime, " triggerTransactionFinalizerHook" );
9096
91- auto jsiDbName = jsi::String::createFromAscii (*runtime, *payload-> dbName );
92- auto jsiEventType = jsi::Value (( int )payload-> event );
97+ auto jsiDbName = jsi::String::createFromAscii (*runtime, *dbName);
98+ auto jsiEventType = jsi::Value (event);
9399 handlerFunction.call (*runtime, move (jsiDbName), move (jsiEventType));
94100 } catch (jsi::JSINativeException e) {
95101 std::cout << e.what () << std::endl;
0 commit comments