Skip to content

Commit b8a68f9

Browse files
committed
Revert mutex change.
1 parent 5dbcb64 commit b8a68f9

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lib/src/sqlite_connection_impl.dart

+20-5
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,30 @@ class SqliteConnectionImpl with SqliteQueries implements SqliteConnection {
123123
@override
124124
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
125125
{Duration? lockTimeout, String? debugContext}) async {
126+
final stopWatch = lockTimeout == null ? null : (Stopwatch()..start());
126127
// Private lock to synchronize this with other statements on the same connection,
127128
// to ensure that transactions aren't interleaved.
128129
return await _connectionMutex.lock(() async {
129-
final ctx = _TransactionContext(_isolateClient);
130-
try {
131-
return await callback(ctx);
132-
} finally {
133-
await ctx.close();
130+
Duration? innerTimeout;
131+
if (lockTimeout != null && stopWatch != null) {
132+
innerTimeout = lockTimeout - stopWatch.elapsed;
133+
stopWatch.stop();
134134
}
135+
// DB lock so that only one write happens at a time
136+
return await _writeMutex.lock(() async {
137+
final ctx = _TransactionContext(_isolateClient);
138+
try {
139+
return await callback(ctx);
140+
} finally {
141+
await ctx.close();
142+
}
143+
}, timeout: innerTimeout).catchError((error, stackTrace) {
144+
if (error is TimeoutException) {
145+
return Future<T>.error(TimeoutException(
146+
'Failed to acquire global write lock', lockTimeout));
147+
}
148+
return Future<T>.error(error, stackTrace);
149+
});
135150
}, timeout: lockTimeout);
136151
}
137152
}

0 commit comments

Comments
 (0)