From 89a96cf14d846b2e2b8e47062125992028b69588 Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sun, 4 Apr 2021 08:23:12 +0300 Subject: [PATCH] StorageLog: avoid UB for rwlock unlock due to unlock from another thread --- src/Storages/StorageLog.cpp | 5 +++++ tests/queries/0_stateless/01796_Log_rwlock_ub.reference | 0 tests/queries/0_stateless/01796_Log_rwlock_ub.sql | 5 +++++ 3 files changed, 10 insertions(+) create mode 100644 tests/queries/0_stateless/01796_Log_rwlock_ub.reference create mode 100644 tests/queries/0_stateless/01796_Log_rwlock_ub.sql diff --git a/src/Storages/StorageLog.cpp b/src/Storages/StorageLog.cpp index ddb0cadc148..9fdd466e792 100644 --- a/src/Storages/StorageLog.cpp +++ b/src/Storages/StorageLog.cpp @@ -357,6 +357,11 @@ void LogBlockOutputStream::writeSuffix() streams.clear(); done = true; + + /// unlock should be done from the same thread as lock, and dtor may be + /// called from different thread, so it should be done here (at least in + /// case of no exceptions occurred) + lock.unlock(); } diff --git a/tests/queries/0_stateless/01796_Log_rwlock_ub.reference b/tests/queries/0_stateless/01796_Log_rwlock_ub.reference new file mode 100644 index 00000000000..e69de29bb2d diff --git a/tests/queries/0_stateless/01796_Log_rwlock_ub.sql b/tests/queries/0_stateless/01796_Log_rwlock_ub.sql new file mode 100644 index 00000000000..4f95dbc14e5 --- /dev/null +++ b/tests/queries/0_stateless/01796_Log_rwlock_ub.sql @@ -0,0 +1,5 @@ +DROP TABLE IF EXISTS underlying_01796; +CREATE TABLE underlying_01796 (key UInt64) Engine=Log(); +INSERT INTO FUNCTION remote('127.1', currentDatabase(), underlying_01796) SELECT toUInt64(number) FROM system.numbers LIMIT 1; +SELECT * FROM underlying_01796 FORMAT Null; +DROP TABLE underlying_01796;