@@ -77,19 +77,25 @@ void transactionFinalizerHandler(const TransactionCallbackPayload *payload) {
77
77
* This function triggers an async invocation to call watch callbacks,
78
78
* avoiding holding SQLite up.
79
79
*/
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] {
81
86
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 ) {
84
90
return ;
85
91
}
86
-
92
+
87
93
auto global = runtime->global ();
88
94
jsi::Function handlerFunction = global.getPropertyAsFunction (
89
95
*runtime, " triggerTransactionFinalizerHook" );
90
96
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);
93
99
handlerFunction.call (*runtime, move (jsiDbName), move (jsiEventType));
94
100
} catch (jsi::JSINativeException e) {
95
101
std::cout << e.what () << std::endl;
0 commit comments