# Zig test step hangs when called with \`--listen=-\`

**URL:** <https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079>\
**Category:** Help\
**Tags:** build-system\
**Created:** [September 20, 2024, 8:55pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079 "2024-09-20T20:55:15Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [September 20, 2024, 8:55pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/1 "2024-09-20T20:55:15Z")

</div>

hi! i’m running into an odd issue with testing my project. when running `zig build test` from the command line, the eventual call to the `test` binary will include a `--listen=-` argument. by comparing against what happens when i run the cached `test` binary directly from the command line without the argument, i’ve discovered that its presence causes the process to hang.

that is to say, using the [default 0.13 test runner](https://github.com/ziglang/zig/blob/0.13.0/lib/compiler/test_runner.zig), my unit tests complete without errors when run with `mainTerminal` but not with `mainServer`.

if i attach a debugger to the running `test` process when called with `zig build test`, it appears that the test runner is blocked in `server.receiveMessage` (and so ultimately in a call to the system `read` function).

in case it’s useful, i’m on 0.13 on a mac running on Apple Silicon. the relevant part of my `build.zig` looks like

```zig
const tests = b.addTest(.{
    .target = target,
    .optimize = optimize,
    .root_source_file = b.path("src/main.zig"),
});
addImports(b, &tests.root_module, target, optimize);
const tests_run = b.addRunArtifact(tests);
const tests_step = b.step("test", "run the zig tests");
tests_step.dependOn(&tests_run.step);

```

(the `addImports` function sets up my project’s dependencies on `ziglua` and `libxev`.)

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 20, 2024, 9:12pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/2 "2024-09-20T21:12:08Z")

</div>

`--listen=-` will make the compiler server listen on standard input, since you don’t need the compiler server, you can remove it.

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [September 20, 2024, 9:16pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/3 "2024-09-20T21:16:35Z")

</div>

sorry, this isn’t super helpful: `zig build test` always appends this argument; i cannot remove it.

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 20, 2024, 9:17pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/4 "2024-09-20T21:17:51Z")

</div>

So `--listen=-` causes the process to hang while running inside `zig test`, not just when running it yourself?

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [September 20, 2024, 9:26pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/5 "2024-09-20T21:26:47Z")

</div>

i don’t think i understand, which indicates i maybe was not clear. i would like to run my unit tests with the zig build system, which always appends the `--listen=-` argument when it calls out to the zig compiler. unfortunately running my tests this way causes the test runner to hang.

i’m beginning to suspect that part of the problem may be my use of a thread pool in libxev. my shutdown code includes the following lines.

```zig
// self.pool is an instance of xev.ThreadPool
self.pool.shutdown();
self.pool.deinit();

```

this sequence of calls appears to complete fine when i test the execution of my program myself, but for some reason appears to not work as expected inside my tests…

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 20, 2024, 9:31pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/6 "2024-09-20T21:31:43Z")

</div>

Using libxev’s thread pool shouldn’t affect stdin. To clarify: `--listen=-` will cause any test to hang if passed by the user, because the user isn’t doing anything with the process’ stdin, but the build system does, so it shouldn’t hang inside `zig build test`. When you remove `--listen=-` and run the `zig test` yourself, does the test complete successfully?

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [September 20, 2024, 10:38pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/7 "2024-09-20T22:38:53Z")

</div>

bizarrely, not when it includes the calls to `shutdown` and `deinit`

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 20, 2024, 10:40pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/8 "2024-09-20T22:40:15Z")

</div>

So is `--listen=-` responsible for the failure?

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [September 20, 2024, 10:43pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/9 "2024-09-20T22:43:37Z")

</div>

i suppose not, although i remain confused. it’s the same calls to `shutdown` and `deinit` that work under normal execution (i.e. `zig build run`) but not from a test

---

<div class="post-metadata">

**Author:** ![cancername](https://ziggit.dev/user_avatar/ziggit.dev/cancername/32/8752_2.png) [@cancername](https://ziggit.dev/u/cancername)\
**Post date:** [September 20, 2024, 10:47pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/10 "2024-09-20T22:47:03Z")

</div>

You’re right, it’s weird. deinit says in its documentation:

> /// Wait for a thread to call shutdown() on the thread pool and kill the worker threads.

So, that means this code should work, but doesn’t. I’ll try to reproduce it.

welp, doesn’t look like that’s happening anytime soon, I can’t seem to build libxev with my zig version.

---

<div class="post-metadata">

**Author:** ![dimdin](https://ziggit.dev/user_avatar/ziggit.dev/dimdin/32/1457_2.png) [@dimdin](https://ziggit.dev/u/dimdin)\
**Post date:** [September 20, 2024, 11:09pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/11 "2024-09-20T23:09:20Z")

</div>

What happens when you run `zig build test -j1` ?

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [September 21, 2024, 2:39am UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/12 "2024-09-21T02:39:49Z")

</div>

the result is the same; the test hangs

---

<div class="post-metadata">

**Author:** ![alanza](https://ziggit.dev/user_avatar/ziggit.dev/alanza/32/6006_2.png) [@alanza](https://ziggit.dev/u/alanza)\
**Post date:** [September 21, 2024, 1:39pm UTC](https://ziggit.dev/t/zig-test-step-hangs-when-called-with-listen/6079/13 "2024-09-21T13:39:42Z")

</div>

ahhh, okay poking around a little more, i think the problem is that the call to `read` never returns—even though i’m cancelling it through `libxev`, there’s still a thread in the thread pool doing something…
