Repository navigation
Expand file tree
/
Copy pathworker_failure.cpp
More file actions
495 lines (452 loc) · 15.7 KB
/
Copy pathworker_failure.cpp
File metadata and controls
495 lines (452 loc) · 15.7 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// Standalone Linux worker-failure tests. See README.md for build instructions.
#include "ThreadPool.h"
#include <cerrno>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dlfcn.h>
#include <pthread.h>
#include <new>
// Fail only allocations on the resizing caller. pthread_create bookkeeping is
// excluded below, so exceptions cannot cross the interposed C API boundary.
thread_local std::size_t allocation_countdown = 0;
thread_local unsigned allocation_rejections = 0;
__attribute__((noinline)) void *operator new(std::size_t size)
{
if (allocation_rejections
|| (allocation_countdown && --allocation_countdown == 0)) {
++allocation_rejections;
throw std::bad_alloc();
}
if (void *p = std::malloc(size ? size : 1))
return p;
throw std::bad_alloc();
}
// Keep replacement allocation functions out of line so GCC does not diagnose
// their malloc/free implementation as mismatched new/delete at call sites.
__attribute__((noinline)) void operator delete(void *p) noexcept { std::free(p); }
__attribute__((noinline)) void operator delete(void *p, std::size_t) noexcept { std::free(p); }
namespace {
const std::chrono::seconds deadline(5);
void require(bool condition, const char *message)
{
if (!condition) {
std::fprintf(stderr, "FAIL: %s\n", message);
// A failed assertion must not hang while unwinding a gated pool.
std::_Exit(EXIT_FAILURE);
}
}
struct Worker {
bool finished = false;
};
struct Tracker {
std::mutex mutex;
std::condition_variable condition;
std::size_t attempts = 0;
std::size_t fail_at = 0;
std::vector<std::shared_ptr<Worker> > workers;
std::vector<std::shared_ptr<Worker> > snapshot()
{
std::lock_guard<std::mutex> lock(mutex);
return workers;
}
std::size_t attempt_count()
{
std::lock_guard<std::mutex> lock(mutex);
return attempts;
}
void wait_finished(const std::shared_ptr<Worker> &worker)
{
std::unique_lock<std::mutex> lock(mutex);
require(condition.wait_for(lock, deadline,
[&] { return worker->finished; }), "worker did not retire");
}
};
Tracker &tracker()
{
static Tracker instance;
return instance;
}
// Only creations requested by the test's main thread are instrumented.
// Other library/runtime threads, including sanitizer helpers, pass through.
thread_local bool track_creation = false;
struct Start {
void *(*function)(void *);
void *argument;
std::shared_ptr<Worker> worker;
};
void *tracked_start(void *argument)
{
std::unique_ptr<Start> start(static_cast<Start *>(argument));
std::shared_ptr<Worker> worker = start->worker;
void *result = start->function(start->argument);
start.reset();
{
std::lock_guard<std::mutex> lock(tracker().mutex);
worker->finished = true;
tracker().condition.notify_all();
}
return result;
}
class FailCreation {
public:
explicit FailCreation(std::size_t nth)
{
std::lock_guard<std::mutex> lock(tracker().mutex);
tracker().fail_at = tracker().attempts + nth;
}
~FailCreation()
{
std::lock_guard<std::mutex> lock(tracker().mutex);
tracker().fail_at = 0;
}
};
void fail_growth(progschj::ThreadPool &pool, std::size_t target,
std::size_t nth = 1)
{
FailCreation failure(nth);
try {
pool.set_pool_size(target);
} catch (const std::system_error &error) {
require(error.code() == std::errc::resource_unavailable_try_again,
"thread creation error was not propagated unchanged");
return;
}
require(false, "growth did not throw the injected error");
}
template <class T>
T get_ready(std::future<T> &future)
{
require(future.wait_for(deadline) == std::future_status::ready,
"task did not finish");
return future.get();
}
void check_task(progschj::ThreadPool &pool)
{
auto future = pool.enqueue([] { return 42; });
require(get_ready(future) == 42, "task returned the wrong value");
}
struct Gate {
std::mutex mutex;
std::condition_variable condition;
std::size_t entered = 0;
bool released = false;
void block()
{
std::unique_lock<std::mutex> lock(mutex);
++entered;
condition.notify_all();
require(condition.wait_for(lock, deadline, [&] { return released; }),
"gate was not released");
}
void wait_entered(std::size_t count)
{
std::unique_lock<std::mutex> lock(mutex);
require(condition.wait_for(lock, deadline,
[&] { return entered == count; }), "not all workers reached gate");
}
void release()
{
std::lock_guard<std::mutex> lock(mutex);
released = true;
condition.notify_all();
}
};
std::vector<std::future<void> > occupy(progschj::ThreadPool &pool,
const std::shared_ptr<Gate> &gate, std::size_t count)
{
std::vector<std::future<void> > futures;
for (std::size_t i = 0; i < count; ++i)
futures.push_back(pool.enqueue([gate] { gate->block(); }));
gate->wait_entered(count);
return futures;
}
void check_parallelism(progschj::ThreadPool &pool, std::size_t count)
{
auto gate = std::make_shared<Gate>();
auto futures = occupy(pool, gate, count);
gate->release();
for (auto &future : futures)
get_ready(future);
}
void construction_failure()
{
// Cover no workers started, one worker started, and several started.
const std::size_t failures[] = {1, 2, 4};
for (std::size_t nth : failures) {
const std::size_t before = tracker().snapshot().size();
bool rejected = false;
{
FailCreation failure(nth);
try {
progschj::ThreadPool pool(4);
} catch (const std::system_error &error) {
require(error.code() == std::errc::resource_unavailable_try_again,
"constructor did not propagate the thread creation error");
rejected = true;
}
}
require(rejected, "constructor did not throw the injected error");
auto workers = tracker().snapshot();
require(workers.size() == before + nth - 1,
"unexpected number of workers started before construction failed");
for (std::size_t i = before; i < workers.size(); ++i)
tracker().wait_finished(workers[i]);
// The exception must be recoverable, including building another pool.
progschj::ThreadPool recovered(2);
check_parallelism(recovered, 2);
}
}
void construction_reserve_failure()
{
const std::size_t attempts = tracker().attempt_count();
bool rejected = false;
{
// Bound the unfixed implementation without exhausting real resources.
FailCreation failure(1);
try {
progschj::ThreadPool pool(std::vector<void *>().max_size() + 1);
} catch (const std::length_error &) {
rejected = true;
} catch (...) {
require(false, "oversized construction did not fail in reserve");
}
}
require(rejected, "oversized construction was not rejected");
require(tracker().attempt_count() == attempts,
"worker creation happened before construction reserve failed");
progschj::ThreadPool recovered(2);
check_parallelism(recovered, 2);
}
void zero_worker_construction()
{
const std::size_t before = tracker().snapshot().size();
std::atomic<unsigned> completed(0);
{
progschj::ThreadPool pool(0);
check_task(pool);
require(tracker().snapshot().size() == before + 1,
"zero construction did not start exactly one worker");
pool.set_pool_size(3);
check_parallelism(pool, 3);
pool.set_pool_size(0);
check_task(pool);
for (unsigned i = 0; i < 100; ++i)
pool.enqueue([&completed] { ++completed; });
}
require(completed == 100, "zero-constructed pool did not drain its tasks");
}
void first_creation_failure()
{
progschj::ThreadPool pool(1);
fail_growth(pool, 3);
// This is the original failure: retrying used the unfulfilled target
// as a worker index, causing an assertion or an invalid detach.
pool.set_pool_size(4);
check_parallelism(pool, 4);
pool.set_pool_size(1);
check_task(pool);
}
void partial_growth_failure()
{
progschj::ThreadPool pool(1);
const std::size_t before = tracker().snapshot().size();
fail_growth(pool, 4, 2);
auto workers = tracker().snapshot();
require(workers.size() == before + 1, "expected one partially added worker");
// The partially started worker becomes the joiner until pool destruction.
check_task(pool);
pool.set_pool_size(4);
check_parallelism(pool, 4);
pool.set_pool_size(1);
pool.set_pool_size(3);
check_parallelism(pool, 3);
}
void growth_during_downsizing()
{
const std::size_t before = tracker().snapshot().size();
progschj::ThreadPool pool(3);
auto workers = tracker().snapshot();
auto gate = std::make_shared<Gate>();
auto futures = occupy(pool, gate, 3);
pool.set_pool_size(1);
// Workers 1 and 2 are still executing. Growth must reuse them before
// attempting (and failing) to create worker 3.
const std::size_t attempts = tracker().attempt_count();
fail_growth(pool, 4);
require(tracker().attempt_count() == attempts + 1,
"growth did not reuse the workers still executing");
require(tracker().snapshot().size() == workers.size() + 1,
"growth unexpectedly created another worker");
gate->release();
for (auto &future : futures)
get_ready(future);
tracker().wait_finished(workers[before + 1]);
tracker().wait_finished(workers[before + 2]);
check_task(pool);
pool.set_pool_size(3);
check_parallelism(pool, 3);
}
void reserve_failure()
{
progschj::ThreadPool pool(1);
const std::size_t attempts = tracker().attempt_count();
bool rejected = false;
{
// Also cap the unfixed implementation: it must not try to exhaust
// real system resources when it misses the reserve check.
FailCreation failure(1);
try {
pool.set_pool_size(std::vector<void *>().max_size() + 1);
} catch (const std::length_error &) {
rejected = true;
} catch (...) {
require(false, "oversized growth did not fail in reserve");
}
}
require(rejected, "oversized growth was not rejected");
require(tracker().attempt_count() == attempts,
"worker creation happened before reserve failed");
check_task(pool);
pool.set_pool_size(3);
check_parallelism(pool, 3);
}
void allocation_failure()
{
{
progschj::ThreadPool pool(3);
const std::size_t attempts = tracker().attempt_count();
allocation_countdown = 1;
bool rejected = false;
try {
pool.set_pool_size(1);
} catch (const std::bad_alloc &) {
rejected = true;
}
const unsigned rejections = allocation_rejections;
allocation_countdown = 0;
allocation_rejections = 0;
require(rejected && rejections == 1, "helper allocation failure was not propagated");
require(tracker().attempt_count() == attempts,
"helper creation followed failed allocation");
check_parallelism(pool, 3);
pool.set_pool_size(1);
check_task(pool);
}
unsigned before_start = 0, after_start = 0;
// Cover reserve, record allocation, and thread-state allocation, including
// failures after one or more workers have started. Further allocations
// keep failing until the resize has propagated its original exception.
for (std::size_t nth = 1; nth <= 12; ++nth) {
progschj::ThreadPool pool(1);
const std::size_t before = tracker().snapshot().size();
allocation_countdown = nth;
bool rejected = false;
try {
pool.set_pool_size(4);
} catch (const std::bad_alloc &) {
rejected = true;
}
const unsigned rejections = allocation_rejections;
allocation_countdown = 0;
allocation_rejections = 0;
if (rejected) {
require(rejections == 1, "failure recovery attempted another allocation");
auto workers = tracker().snapshot();
if (workers.size() == before)
++before_start;
else {
++after_start;
// The last addition becomes the helper; earlier ones retire.
for (std::size_t i = before; i + 1 < workers.size(); ++i)
tracker().wait_finished(workers[i]);
}
check_task(pool);
}
pool.set_pool_size(4);
check_parallelism(pool, 4);
pool.set_pool_size(1);
check_task(pool);
}
require(before_start && after_start, "allocation injection missed partial growth");
}
void resizing_with_tasks()
{
const std::size_t batches = 100;
const std::size_t batch_size = 30;
std::unique_ptr<std::atomic<unsigned>[]> counts(
new std::atomic<unsigned>[batches * batch_size]);
for (std::size_t i = 0; i < batches * batch_size; ++i)
counts[i].store(0);
{
progschj::ThreadPool pool(2);
for (std::size_t batch = 0; batch < batches; ++batch) {
pool.set_pool_size(1 + batch % 6);
for (std::size_t j = 0; j < batch_size; ++j) {
const std::size_t i = batch * batch_size + j;
pool.enqueue([&counts, i] { ++counts[i]; });
}
}
// Destruction must finish and drain all queued work.
}
for (std::size_t i = 0; i < batches * batch_size; ++i)
require(counts[i] == 1, "queued task did not execute exactly once");
}
} // namespace
extern "C" int pthread_create(pthread_t *thread, const pthread_attr_t *attributes,
void *(*function)(void *), void *argument)
{
static auto real_create = reinterpret_cast<decltype(&pthread_create)>(
dlsym(RTLD_NEXT, "pthread_create"));
require(real_create != nullptr, "could not resolve pthread_create");
if (!track_creation)
return real_create(thread, attributes, function, argument);
const std::size_t saved_countdown = allocation_countdown;
allocation_countdown = 0;
std::lock_guard<std::mutex> lock(tracker().mutex);
if (++tracker().attempts == tracker().fail_at) {
allocation_countdown = saved_countdown;
return EAGAIN;
}
auto worker = std::make_shared<Worker>();
std::unique_ptr<Start> start(new Start{function, argument, worker});
tracker().workers.push_back(worker);
const int result = real_create(thread, attributes, tracked_start, start.get());
if (result == 0)
start.release();
else
tracker().workers.pop_back();
allocation_countdown = saved_countdown;
return result;
}
int main(int argc, char **argv)
{
track_creation = true;
const struct Test {
const char *name;
void (*run)();
} tests[] = {
{"construct", construction_failure},
{"construct-reserve", construction_reserve_failure},
{"zero", zero_worker_construction},
{"first", first_creation_failure},
{"partial", partial_growth_failure},
{"downsizing", growth_during_downsizing},
{"reserve", reserve_failure},
{"allocation", allocation_failure},
{"tasks", resizing_with_tasks}
};
bool ran = false;
for (const auto &test : tests) {
if (argc > 1 && std::strcmp(argv[1], test.name) != 0)
continue;
ran = true;
test.run();
// Also keep bookkeeping alive when testing older, detaching revisions.
for (const auto &worker : tracker().snapshot())
tracker().wait_finished(worker);
std::printf("PASS: %s\n", test.name);
}
require(ran, "unknown test name");
}