Skip to content

fix(launcher): never leave a process stream unread, and decode output as UTF-8 - #115

Open
Macbet wants to merge 2 commits into
masterfrom
fix/launcher-stream-draining
Open

Macbet wants to merge 2 commits into
masterfrom
fix/launcher-stream-draining

Conversation

@Macbet

@Macbet Macbet commented Nov 18, 2026

Copy link
Copy Markdown

Problem

  • Hang on an unread stream. ProcessLauncher only started a reader for a stream that had a listener. errorListener may be null, and the no-arg client methods use whatever listeners are set. Once the child wrote ~64 KB to an unread pipe it blocked in write() forever. Reproduced with 300 KB on stderr and no error listener; TF_LOG=DEBUG is a realistic trigger.
  • A throwing listener killed terraform. The exception ended the reader, which closed the pipe, and the child died from SIGPIPE mid-run. For terraform that can be mid-apply, possibly before it releases the state lock.
  • Wrong charset. Output was decoded with the platform charset, so on Java 17 under LANG=C terraform's ╷ │ ╵ frame and any non-ASCII text became ?.

Changes

  • A stream without a listener goes to Redirect.DISCARD. A stream with a listener is explicitly set to PIPE, so a launch retried after a failed start() doesn't keep a stale DISCARD.
  • On the first listener exception, or an Error from readLine():
    • the failure is recorded and the listener gets no more lines;
    • the rest of the stream is drained as raw bytes, without building strings;
    • once the process has exited, the future fails with that exception as its cause, taking precedence over the exit code.
    • On the timeout path the TimeoutException is kept, with the listener failure added as suppressed.
  • asserts, which are disabled in production, are replaced with explicit checks. An empty environment variable name is logged and ignored, because it used to be accepted.
  • Output is read as UTF-8. A dedicated surefire execution reruns the regression test with -Dfile.encoding=US-ASCII, since on JDK 18+ or under C.UTF-8 the default run can't catch it.

Caller-visible change

A throwing listener already failed the future before this change. The cause is now the listener's own exception instead of RuntimeException("Failed to capture process output stream"), and the process runs to completion.

Tests

  • Docker, maven:3.9-eclipse-temurin-17, --init: 44 tests in terraform-client plus the US-ASCII execution plus the sample pass, in both the default locale and LANG=C.
  • Every new test fails on the previous code.
  • The US-ASCII execution fails on the old charset code on JDK 17 and 21.

A stream without a listener got no reader, so once the child wrote ~64 KB
to it, it blocked in write() forever. Such streams are now redirected to
DISCARD, and streams with a listener explicitly to PIPE so a launch retried
after a failed start() does not keep a stale DISCARD.

A throwing listener ended its reader, closing the pipe and killing the
child with SIGPIPE mid-run. On the first exception from the listener (or
an Error from readLine()) the failure is recorded, the listener gets no
further lines, and the rest of the stream is drained as raw bytes. Once
the process has exited the launch future fails with that exception as its
cause, taking precedence over the exit code; a CompletionException from
the listener is not wrapped again. On the timeout path the
TimeoutException is kept, with the failure added as suppressed and logged.

Also replace the asserts, which are disabled in production, with explicit
checks: IllegalStateException after launch, NullPointerException for a
null executor or variable name. An empty variable name is logged and
ignored rather than rejected, since it used to be accepted.
Output was decoded with the platform charset, so on Java 17 under LANG=C
terraform's box-drawing frame and any non-ASCII text became '?'.

A dedicated surefire execution reruns the regression test with
-Dfile.encoding=US-ASCII: under C.UTF-8 or on JDK 18+ the default charset
already is UTF-8, so the default execution alone cannot catch it.
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant