Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/core/database/mysql_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,12 @@ class MysqlConnection {
if (!isConnected || _conn == null) {
throw StateError('Not connected to MySQL');
}
return _conn!.execute(sql, params, iterable);
try {
return await _conn!.execute(sql, params, iterable);
} on TimeoutException {
unawaited(forceClose());
rethrow;
}
}

/// Runs [execute] with an application-level [timeout] (driver limits still apply).
Expand All @@ -282,7 +287,12 @@ class MysqlConnection {
}) async {
final f = execute(sql, params, iterable);
if (timeout == null) return f;
return f.timeout(timeout);
try {
return await f.timeout(timeout);
} on TimeoutException {
unawaited(forceClose());
rethrow;
}
}

/// Lists user-visible databases (excludes typical system schemas).
Expand Down
23 changes: 22 additions & 1 deletion lib/core/database/postgres_connection.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io' show SecurityContext;

import 'package:flutter/foundation.dart';
Expand Down Expand Up @@ -252,7 +253,27 @@ class PostgresConnection {
if (!isConnected || _conn == null) {
throw StateError('Not connected to PostgreSQL');
}
return _conn!.execute(sql, timeout: timeout);
try {
return await _conn!.execute(sql, timeout: timeout);
} on TimeoutException {
unawaited(forceClose());
rethrow;
}
}

/// Runs [execute] with an application-level [timeout] (in addition to driver timeout).
Future<Result> executeWithTimeout(
String sql, {
Duration? timeout,
}) async {
final f = execute(sql, timeout: timeout);
if (timeout == null) return f;
try {
return await f.timeout(timeout);
} on TimeoutException {
unawaited(forceClose());
rethrow;
}
}

/// Whether the session has an open transaction (PostgreSQL 13+).
Expand Down
31 changes: 26 additions & 5 deletions lib/core/database/sqlite_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,32 @@ class SqliteConnection {
throw StateError('Database connection is read-only');
}

if (isReadOnlyQuery || hasReturning) {
return await _db!.rawQuery(sql, arguments);
} else {
await _db!.execute(sql, arguments);
return [];
try {
if (isReadOnlyQuery || hasReturning) {
return await _db!.rawQuery(sql, arguments);
} else {
await _db!.execute(sql, arguments);
return [];
}
} on TimeoutException {
unawaited(forceClose());
rethrow;
}
}

/// Runs [execute] with an application-level [timeout].
Future<List<Map<String, Object?>>> executeWithTimeout(
String sql, {
Duration? timeout,
List<Object?>? arguments,
}) async {
final f = execute(sql, arguments);
if (timeout == null) return f;
try {
return await f.timeout(timeout);
} on TimeoutException {
unawaited(forceClose());
rethrow;
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/features/mysql/mysql_sql_workspace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ class _MysqlSqlWorkspaceState extends material.State<MysqlSqlWorkspace> {
);
}
} on TimeoutException catch (e) {
unawaited(_lease?.connection.forceClose());
if (mounted) {
setState(() {
_error = e.toString();
Expand Down
16 changes: 16 additions & 0 deletions lib/features/postgresql/postgres_sql_workspace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ class _PostgresSqlWorkspaceState extends material.State<PostgresSqlWorkspace> {
_statusLine = 'OK: $cmd';
_running = false;
});
} on TimeoutException catch (e) {
unawaited(_lease?.connection.forceClose());
if (mounted) {
setState(() {
_error = 'Query timed out: ${e.message ?? e}';
_running = false;
});
}
} on pg.ServerException catch (e) {
if (mounted) {
setState(() {
Expand Down Expand Up @@ -375,6 +383,14 @@ class _PostgresSqlWorkspaceState extends material.State<PostgresSqlWorkspace> {
),
);
}
} on TimeoutException catch (e) {
unawaited(_lease?.connection.forceClose());
if (mounted) {
setState(() {
_error = 'Query timed out: ${e.message ?? e}';
_running = false;
});
}
} on pg.ServerException catch (e) {
if (mounted) {
setState(() {
Expand Down
8 changes: 8 additions & 0 deletions lib/features/sqlite/sqlite_sql_workspace.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ class _SqliteSqlWorkspaceState extends material.State<SqliteSqlWorkspace> {
),
);
}
} on TimeoutException catch (e) {
unawaited(_lease?.connection.forceClose());
if (mounted) {
setState(() {
_error = 'Query timed out: ${e.message ?? e}';
_running = false;
});
}
} catch (e) {
if (mounted) {
setState(() {
Expand Down
157 changes: 157 additions & 0 deletions test/core/database/connection_timeout_protocol_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:mysql_client/mysql_client.dart';
import 'package:querya_desktop/core/database/mysql_connection.dart';
import 'package:querya_desktop/core/database/postgres_connection.dart';
import 'package:querya_desktop/core/database/sqlite_connection.dart';
import 'package:postgres/postgres.dart' as pg;

class FakeSlowMysqlConnection extends MysqlConnection {
FakeSlowMysqlConnection({super.id = 1})
: super(
name: 'fake_slow_mysql',
host: 'localhost',
port: 3306,
database: 'testdb',
);

bool _connected = true;
int forceCloseCount = 0;

@override
bool get isConnected => _connected;

@override
Future<IResultSet> execute(
String sql, [
Map<String, dynamic>? params,
bool iterable = false,
]) async {
await Future.delayed(const Duration(seconds: 10));
throw Exception('should not reach here');
}

@override
Future<void> forceClose() async {
forceCloseCount++;
_connected = false;
}
}

class FakeSlowPostgresConnection extends PostgresConnection {
FakeSlowPostgresConnection({super.id = 1})
: super(
name: 'fake_slow_pg',
host: 'localhost',
port: 5432,
database: 'postgres',
);

bool _connected = true;
int forceCloseCount = 0;

@override
bool get isConnected => _connected;

@override
Future<pg.Result> execute(String sql, {Duration? timeout}) async {
await Future.delayed(const Duration(seconds: 10));
throw Exception('should not reach here');
}

@override
Future<void> forceClose() async {
forceCloseCount++;
_connected = false;
}
}

class FakeSlowSqliteConnection extends SqliteConnection {
FakeSlowSqliteConnection({super.id = 1})
: super(
name: 'fake_slow_sqlite',
path: ':memory:',
);

bool _connected = true;
int forceCloseCount = 0;

@override
bool get isConnected => _connected;

@override
Future<List<Map<String, Object?>>> execute(
String sql, [
List<Object?>? arguments,
]) async {
await Future.delayed(const Duration(seconds: 10));
throw Exception('should not reach here');
}

@override
Future<void> forceClose() async {
forceCloseCount++;
_connected = false;
}
}

void main() {
group('Connection Timeout Protocol Protection', () {
test('MysqlConnection.executeWithTimeout force-closes on timeout', () async {
final conn = FakeSlowMysqlConnection();
expect(conn.isConnected, isTrue);

try {
await conn.executeWithTimeout(
'SELECT sleep(100)',
timeout: const Duration(milliseconds: 20),
);
fail('Should have thrown TimeoutException');
} on TimeoutException {
// Expected
}

await Future.delayed(const Duration(milliseconds: 10));
expect(conn.forceCloseCount, 1);
expect(conn.isConnected, isFalse);
});

test('PostgresConnection.executeWithTimeout force-closes on TimeoutException', () async {
final conn = FakeSlowPostgresConnection();
expect(conn.isConnected, isTrue);

try {
await conn.executeWithTimeout(
'SELECT pg_sleep(100)',
timeout: const Duration(milliseconds: 20),
);
fail('Should have thrown TimeoutException');
} on TimeoutException {
// Expected
}

await Future.delayed(const Duration(milliseconds: 10));
expect(conn.forceCloseCount, 1);
expect(conn.isConnected, isFalse);
});

test('SqliteConnection.executeWithTimeout force-closes on TimeoutException', () async {
final conn = FakeSlowSqliteConnection();
expect(conn.isConnected, isTrue);

try {
await conn.executeWithTimeout(
'SELECT 1',
timeout: const Duration(milliseconds: 20),
);
fail('Should have thrown TimeoutException');
} on TimeoutException {
// Expected
}

await Future.delayed(const Duration(milliseconds: 10));
expect(conn.forceCloseCount, 1);
expect(conn.isConnected, isFalse);
});
});
}
Loading