Skip to content

Commit

Permalink
Terminate fuzzer workers in atexit() handler
Browse files Browse the repository at this point in the history
  • Loading branch information
senier committed May 25, 2024
1 parent 4bed7a9 commit 4d17fc6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Crash result simplification (#30)
- Configurable simplification steps (#54)
- Parallel simplification (#53)
- Terminate fuzzer workers in atexit() handler

### Changed

Expand Down
8 changes: 8 additions & 0 deletions cobrafuzz/fuzzer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import atexit
import hashlib
import io
import logging
Expand Down Expand Up @@ -391,16 +392,23 @@ def _initialize_process(self, wid: int) -> tuple[MPProcess, mp.Queue[Update]]:
return result, queue

def _terminate_workers(self) -> None:
if not self._workers:
return

self._result_queue.cancel_join_thread()

for p, q in self._workers:
q.cancel_join_thread()
p.terminate()
p.join(timeout=1)

del self._workers[:]

def start(self) -> None: # noqa: PLR0912
start_time = time.time()

self._workers = [self._initialize_process(wid=wid) for wid in range(self._num_workers)]
atexit.register(self._terminate_workers)

logging.info(
"START units: %d, workers: %d, seeds: %d",
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_fuzzer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import copy
import hashlib
import logging
import multiprocessing
Expand Down Expand Up @@ -651,6 +652,10 @@ def target(_: bytes) -> None:
w[0].terminated and w[0].joined and w[0].timeout == 1 and w[1].canceled for w in workers
)

previous_workers = copy.copy(workers)
f._terminate_workers() # noqa: SLF001
assert workers == previous_workers


def test_worker_run_ignored_exception() -> None:
class Error:
Expand Down

0 comments on commit 4d17fc6

Please sign in to comment.