-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_room_process_invalidation.cpp
More file actions
214 lines (170 loc) · 6.21 KB
/
Copy pathtest_room_process_invalidation.cpp
File metadata and controls
214 lines (170 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright 2026 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// @file test_room_process_invalidation.cpp
/// @brief Linux integration repro for server-driven room invalidation at process exit.
///
/// Reproduces the user-reported shutdown path: a C++ client joins a room, the server
/// invalidates the session (duplicate identity), and the client process exits without
/// calling Room::disconnect() or livekit::shutdown(). On glibc/Linux, heap corruption
/// during thread teardown can surface as:
/// SIGABRT: tcache_thread_shutdown(): unaligned tcache chunk detected
#include <gtest/gtest.h>
#include <livekit/livekit.h>
#include <cstdlib>
#include <string>
#if defined(__linux__)
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <cerrno>
#include <chrono>
#include <condition_variable>
#include <csignal>
#include <cstring>
#include <mutex>
#include <thread>
#endif
namespace livekit::test {
#if defined(__linux__)
namespace {
using namespace std::chrono_literals;
constexpr char kChildReadyByte = 1;
constexpr auto kChildConnectTimeout = 30s;
constexpr auto kInvalidationTimeout = 30s;
constexpr auto kChildExitTimeout = 30s;
struct ProcessInvalidationConfig {
std::string url;
std::string token;
bool available = false;
static ProcessInvalidationConfig fromEnv() {
ProcessInvalidationConfig config;
const char* url = std::getenv("LIVEKIT_URL");
const char* token = std::getenv("LIVEKIT_TOKEN_A");
if (url != nullptr && token != nullptr && url[0] != '\0' && token[0] != '\0') {
config.url = url;
config.token = token;
config.available = true;
}
return config;
}
};
class DuplicateIdentityWaiter : public RoomDelegate {
public:
void onDisconnected(Room&, const DisconnectedEvent& event) override {
{
const std::scoped_lock<std::mutex> lock(mutex_);
reason_ = event.reason;
disconnected_ = true;
}
cv_.notify_all();
}
bool waitForDuplicateIdentity(std::chrono::milliseconds timeout) {
std::unique_lock<std::mutex> lock(mutex_);
return cv_.wait_for(lock, timeout,
[this]() { return disconnected_ && reason_ == DisconnectReason::DuplicateIdentity; });
}
private:
mutable std::mutex mutex_;
std::condition_variable cv_;
bool disconnected_ = false;
DisconnectReason reason_ = DisconnectReason::Unknown;
};
bool writeByte(int fd, char value) { return ::write(fd, &value, 1) == 1; }
bool readByte(int fd, char* value) { return ::read(fd, value, 1) == 1; }
void runVictimChild(int ready_pipe_write, const std::string& url, const std::string& token) {
if (!livekit::initialize(livekit::LogLevel::Info)) {
_exit(2);
}
Room room;
DuplicateIdentityWaiter delegate;
room.setDelegate(&delegate);
RoomOptions options;
if (!room.connect(url, token, options)) {
_exit(3);
}
if (!writeByte(ready_pipe_write, kChildReadyByte)) {
_exit(4);
}
if (!delegate.waitForDuplicateIdentity(kInvalidationTimeout)) {
_exit(5);
}
// Deliberately skip Room::disconnect() and livekit::shutdown() to mirror the
// user report: process exit tears down Rust/WebRTC threads and glibc allocators.
std::exit(0);
}
} // namespace
class RoomInvalidationProcessExitTest : public ::testing::Test {};
TEST_F(RoomInvalidationProcessExitTest, DuplicateIdentityInvalidationThenClientProcessExit) {
const ProcessInvalidationConfig config = ProcessInvalidationConfig::fromEnv();
if (!config.available) {
GTEST_SKIP() << "LIVEKIT_URL and LIVEKIT_TOKEN_A must be set";
}
int ready_pipe[2] = {-1, -1};
ASSERT_EQ(::pipe(ready_pipe), 0) << "pipe() failed: " << std::strerror(errno);
const pid_t child_pid = ::fork();
ASSERT_NE(child_pid, -1) << "fork() failed: " << std::strerror(errno);
if (child_pid == 0) {
::close(ready_pipe[0]);
runVictimChild(ready_pipe[1], config.url, config.token);
::close(ready_pipe[1]);
_exit(1);
}
::close(ready_pipe[1]);
char ready_byte = 0;
ASSERT_TRUE(readByte(ready_pipe[0], &ready_byte)) << "timed out waiting for child connect signal";
::close(ready_pipe[0]);
ASSERT_EQ(ready_byte, kChildReadyByte);
ASSERT_TRUE(livekit::initialize(livekit::LogLevel::Info)) << "parent initialize failed";
Room usurper;
RoomOptions options;
ASSERT_TRUE(usurper.connect(config.url, config.token, options))
<< "parent failed to connect with duplicate identity token";
int status = 0;
const auto wait_deadline = std::chrono::steady_clock::now() + kChildExitTimeout;
while (true) {
const pid_t waited = ::waitpid(child_pid, &status, WNOHANG);
if (waited == child_pid) {
break;
}
if (waited == -1) {
FAIL() << "waitpid() failed: " << std::strerror(errno);
}
if (std::chrono::steady_clock::now() >= wait_deadline) {
::kill(child_pid, SIGKILL);
(void)::waitpid(child_pid, &status, 0);
FAIL() << "timed out waiting for victim child process to exit";
}
std::this_thread::sleep_for(50ms);
}
usurper.disconnect();
livekit::shutdown();
if (WIFSIGNALED(status)) {
const int signal = WTERMSIG(status);
FAIL() << "victim child terminated by signal " << signal
<< " (expected clean exit 0; SIGABRT=6 often indicates glibc tcache corruption on shutdown)";
}
if (!WIFEXITED(status)) {
FAIL() << "victim child did not exit normally";
}
EXPECT_EQ(WEXITSTATUS(status), 0) << "victim child should exit 0 after duplicate-identity invalidation";
}
#else
class RoomInvalidationProcessExitTest : public ::testing::Test {};
TEST_F(RoomInvalidationProcessExitTest, DuplicateIdentityInvalidationThenClientProcessExit) {
GTEST_SKIP() << "Linux-only repro: glibc tcache_thread_shutdown() abort is not observable on this platform";
}
#endif
} // namespace livekit::test