Skip to content

signal: Write fatal signal backtraces with write(2) - #49207

Merged
phlax merged 4 commits into
envoyproxy:mainfrom
phlax:signal-safe-stderr
Nov 12, 2026
Merged

phlax merged 4 commits into
envoyproxy:mainfrom
phlax:signal-safe-stderr

Conversation

@phlax

@phlax phlax commented Nov 12, 2026

Copy link
Copy Markdown
Member

#48471 took the fatal signal handler's output off the logger, which could deadlock or abort on the logger mutex, and sent it to std::cerr. That is not async-signal-safe either: iostream output goes through the stdio lock and is not guaranteed to be allocation free, so a signal arriving while the same thread is inside a stream or allocator call can still hang or fault again before the backtrace and the fatal actions run.

This adds StderrWriter, which builds a line in a fixed buffer and writes it with a single write(2), and uses it for the fault line and for the regular and coroutine backtraces when they are directed to stderr. The output is unchanged.

printTrace() goes back to taking only a stream, as its prefixed form was only used for this path.

Not covered: the fatal error handlers that run after the backtrace are still handed std::cerr, as before.

Risk Level: low
Testing: unit test for StderrWriter; the existing Backward.FaultToStderr, Backward.TraceToStderr and coroutine stderr tests cover the output format
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The fixed-buffer formatter changes MSVC pointer formatting, omitting its zero-padded uppercase hexadecimal representation without a 0x prefix and causing two tests to fail on that platform.

1 open issue
What changed in this PR

This PR hardens Envoy’s fatal-signal diagnostics by replacing iostream-based stderr output with a fixed-buffer writer, continuing the work in #48471 to avoid logging-related deadlocks.

Changes:

  • Adds StderrWriter for buffered stderr writes without allocation or iostream locks.
  • Migrates fault messages and regular/coroutine backtraces to the writer, simplifying printTrace().
  • Adds formatting, flushing, and truncation tests with build dependencies.
File Description
test/​common/​common/​stderr_writer_test.cc Tests formatting, null text, flushing, and truncation.
test/​common/​common/​BUILD Registers the writer’s unit tests.
source/​server/​BUILD Adds the writer dependency for server backtraces.
source/​server/​backtrace.h Uses the writer for stderr fault messages and backtraces.
source/​common/​coroutine/​BUILD Adds the writer dependency for coroutine backtraces.
source/​common/​coroutine/​backtrace.h Uses the writer for stderr coroutine backtraces.
source/​common/​common/​stderr_writer.h Implements fixed-buffer formatting and stderr writes.
source/​common/​common/​BUILD Defines the writer library target.

Review effort: Balanced

Comment thread source/common/common/stderr_writer.h Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The tests cover only successful writes, leaving short-write handling, EINTR retries, and buffer reset after permanent errors unverified for fatal-signal diagnostics.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread source/common/common/stderr_writer.h Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the fixed-buffer stderr backtrace change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

envoyproxy#48471 took the fatal signal handler's output off the logger, which could
deadlock or abort on the logger mutex, and sent it to `std::cerr`. That
is not async-signal-safe either: iostream output goes through the stdio
lock and is not guaranteed to be allocation free, so a signal arriving
while the same thread is inside a stream or allocator call can still hang
or fault again before the backtrace and the fatal actions run.

Add `StderrWriter`, which builds a line in a fixed buffer and writes it
with a single write(2), and use it for the fault line and for the regular
and coroutine backtraces when they are directed to stderr. The output is
unchanged.

`printTrace()` goes back to taking only a stream, as its prefixed form
was only used for this path.

Signed-off-by: Ryan Northey <[email protected]>
`writeStderr()` formats a line into a buffer on the stack with
`absl::SNPrintF` and writes it with a single write(2), in place of the
stream-like writer and its own number formatting.

A null faulting address is now shown as `(nil)`.

Signed-off-by: Ryan Northey <[email protected]>
The regular and coroutine backtraces wrote their frames to stderr with
the same block, which is now `writeStderrFrame()`.

Signed-off-by: Ryan Northey <[email protected]>
ravenblackx
ravenblackx previously approved these changes Nov 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The stderr helper breaks Windows compilation through unguarded POSIX dependencies and can lose diagnostic output because it does not handle short writes or retry EINTR.

2 open issues

Review effort: Balanced

Comment thread source/common/common/stderr_writer.h Resolved
Comment thread source/common/common/stderr_writer.h Resolved
A line written from the fatal signal handler was lost in part on a short
write, and entirely if the write was interrupted.

Signed-off-by: Ryan Northey <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Fatal-signal safety depends on absl::SNPrintF internals unavailable in this checkout, and output tests do not establish allocation or lock freedom, requiring a source audit or interruption-focused validation.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@phlax

phlax commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

Source-level check of the fatal signal path, against the pinned abseil (20260817.0):

  • absl::SNPrintF with %s/%d/%p does not allocate, lock or touch stdio: the format literal is parsed in place, and output goes through an on-stack FormatSinkImpl into a bounded memcpy sink. The allocating code in str_format (Summarize, FormatPack, Streamable, ParsedFormat, wide strings) is not reachable from it.
  • writeStderr() adds only write(2), which is continued on a short write and retried on EINTR.
  • Everything else from handler entry to the end of the coroutine backtrace is unchanged and was already safe: sigdescr_np, the pre-initialized VersionInfo::version() and addrMapping(), absl::GetStackTraceWithContext, and absl::Symbolize (its own signal-safe arena).

So the backtrace output no longer depends on iostreams, the stdio lock or the allocator, and nothing is worse than before: the output is the same other than a null faulting address showing as (nil), and a line longer than the 4608 byte buffer is truncated.

Not covered here, and not changed: after the backtraces the handler still passes std::cerr to FatalErrorHandler::callFatalErrorHandlers() and uses it for the "FatalActions triggered a fatal signal" line. The handler as a whole is not async-signal-safe yet.

@ravenblackx

Copy link
Copy Markdown
Contributor

/retest because //test/extensions/filters/http/ext_proc/http_client:ext_proc_http_integration_test is a sack.

@phlax

phlax commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

/retest

phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
could deadlock or abort on the logger mutex, and sent it to `std::cerr`.
That is not async-signal-safe either: iostream output goes through the
stdio lock and is not guaranteed to be allocation free, so a signal
arriving while the same thread is inside a stream or allocator call can
still hang or fault again before the backtrace and the fatal actions
run.

This adds `StderrWriter`, which builds a line in a fixed buffer and
writes it with a single `write(2)`, and uses it for the fault line and
for the regular and coroutine backtraces when they are directed to
stderr. The output is unchanged.

`printTrace()` goes back to taking only a stream, as its prefixed form
was only used for this path.

Not covered: the fatal error handlers that run after the backtrace are
still handed `std::cerr`, as before.

Risk Level: low
Testing: unit test for `StderrWriter`; the existing
`Backward.FaultToStderr`, `Backward.TraceToStderr` and coroutine stderr
tests cover the output format
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

---------

Backport note: lowers the `source/server` coverage threshold to 93.2, as this
change takes that directory from 93.3% to 93.2% on this branch.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
could deadlock or abort on the logger mutex, and sent it to `std::cerr`.
That is not async-signal-safe either: iostream output goes through the
stdio lock and is not guaranteed to be allocation free, so a signal
arriving while the same thread is inside a stream or allocator call can
still hang or fault again before the backtrace and the fatal actions
run.

This adds `StderrWriter`, which builds a line in a fixed buffer and
writes it with a single `write(2)`, and uses it for the fault line and
for the regular and coroutine backtraces when they are directed to
stderr. The output is unchanged.

`printTrace()` goes back to taking only a stream, as its prefixed form
was only used for this path.

Not covered: the fatal error handlers that run after the backtrace are
still handed `std::cerr`, as before.

Risk Level: low
Testing: unit test for `StderrWriter`; the existing
`Backward.FaultToStderr`, `Backward.TraceToStderr` and coroutine stderr
tests cover the output format
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

---------

Backport note: lowers the `source/server` coverage threshold to 93.2, as this
change takes that directory from 93.3% to 93.2% on this branch.

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 13, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
could deadlock or abort on the logger mutex, and sent it to `std::cerr`.
That is not async-signal-safe either: iostream output goes through the
stdio lock and is not guaranteed to be allocation free, so a signal
arriving while the same thread is inside a stream or allocator call can
still hang or fault again before the backtrace and the fatal actions
run.

This adds `StderrWriter`, which builds a line in a fixed buffer and
writes it with a single `write(2)`, and uses it for the fault line and
for the regular and coroutine backtraces when they are directed to
stderr. The output is unchanged.

`printTrace()` goes back to taking only a stream, as its prefixed form
was only used for this path.

Not covered: the fatal error handlers that run after the backtrace are
still handed `std::cerr`, as before.

Risk Level: low
Testing: unit test for `StderrWriter`; the existing
`Backward.FaultToStderr`, `Backward.TraceToStderr` and coroutine stderr
tests cover the output format
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

---------

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 16, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit that referenced this pull request Nov 17, 2026
`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as #48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on #49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 17, 2026
could deadlock or abort on the logger mutex, and sent it to `std::cerr`.
That is not async-signal-safe either: iostream output goes through the
stdio lock and is not guaranteed to be allocation free, so a signal
arriving while the same thread is inside a stream or allocator call can
still hang or fault again before the backtrace and the fatal actions
run.

This adds `StderrWriter`, which builds a line in a fixed buffer and
writes it with a single `write(2)`, and uses it for the fault line and
for the regular and coroutine backtraces when they are directed to
stderr. The output is unchanged.

`printTrace()` goes back to taking only a stream, as its prefixed form
was only used for this path.

Not covered: the fatal error handlers that run after the backtrace are
still handed `std::cerr`, as before.

Risk Level: low
Testing: unit test for `StderrWriter`; the existing
`Backward.FaultToStderr`, `Backward.TraceToStderr` and coroutine stderr
tests cover the output format
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

---------

Backport note: lowers the `source/server` coverage threshold to 93.2, as this
change takes that directory from 93.3% to 93.2% on this branch.

(cherry picked from commit 1e35e9c)

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 17, 2026
could deadlock or abort on the logger mutex, and sent it to `std::cerr`.
That is not async-signal-safe either: iostream output goes through the
stdio lock and is not guaranteed to be allocation free, so a signal
arriving while the same thread is inside a stream or allocator call can
still hang or fault again before the backtrace and the fatal actions
run.

This adds `StderrWriter`, which builds a line in a fixed buffer and
writes it with a single `write(2)`, and uses it for the fault line and
for the regular and coroutine backtraces when they are directed to
stderr. The output is unchanged.

`printTrace()` goes back to taking only a stream, as its prefixed form
was only used for this path.

Not covered: the fatal error handlers that run after the backtrace are
still handed `std::cerr`, as before.

Risk Level: low
Testing: unit test for `StderrWriter`; the existing
`Backward.FaultToStderr`, `Backward.TraceToStderr` and coroutine stderr
tests cover the output format
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

---------

Backport note: lowers the `source/server` coverage threshold to 93.2, as this
change takes that directory from 93.3% to 93.2% on this branch.

(cherry picked from commit 1e35e9c)

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 17, 2026
could deadlock or abort on the logger mutex, and sent it to `std::cerr`.
That is not async-signal-safe either: iostream output goes through the
stdio lock and is not guaranteed to be allocation free, so a signal
arriving while the same thread is inside a stream or allocator call can
still hang or fault again before the backtrace and the fatal actions
run.

This adds `StderrWriter`, which builds a line in a fixed buffer and
writes it with a single `write(2)`, and uses it for the fault line and
for the regular and coroutine backtraces when they are directed to
stderr. The output is unchanged.

`printTrace()` goes back to taking only a stream, as its prefixed form
was only used for this path.

Not covered: the fatal error handlers that run after the backtrace are
still handed `std::cerr`, as before.

Risk Level: low
Testing: unit test for `StderrWriter`; the existing
`Backward.FaultToStderr`, `Backward.TraceToStderr` and coroutine stderr
tests cover the output format
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

---------

(cherry picked from commit 1e35e9c)

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 17, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit that referenced this pull request Nov 17, 2026
could deadlock or abort on the logger mutex, and sent it to `std::cerr`.
That is not async-signal-safe either: iostream output goes through the
stdio lock and is not guaranteed to be allocation free, so a signal
arriving while the same thread is inside a stream or allocator call can
still hang or fault again before the backtrace and the fatal actions
run.

This adds `StderrWriter`, which builds a line in a fixed buffer and
writes it with a single `write(2)`, and uses it for the fault line and
for the regular and coroutine backtraces when they are directed to
stderr. The output is unchanged.

`printTrace()` goes back to taking only a stream, as its prefixed form
was only used for this path.

Not covered: the fatal error handlers that run after the backtrace are
still handed `std::cerr`, as before.

Risk Level: low
Testing: unit test for `StderrWriter`; the existing
`Backward.FaultToStderr`, `Backward.TraceToStderr` and coroutine stderr
tests cover the output format
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

---------

Backport note: lowers the `source/server` coverage threshold to 93.2, as this
change takes that directory from 93.3% to 93.2% on this branch.

(cherry picked from commit 1e35e9c)

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit that referenced this pull request Nov 17, 2026
`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as #48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on #49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 18, 2026
could deadlock or abort on the logger mutex, and sent it to `std::cerr`.
That is not async-signal-safe either: iostream output goes through the
stdio lock and is not guaranteed to be allocation free, so a signal
arriving while the same thread is inside a stream or allocator call can
still hang or fault again before the backtrace and the fatal actions
run.

This adds `StderrWriter`, which builds a line in a fixed buffer and
writes it with a single `write(2)`, and uses it for the fault line and
for the regular and coroutine backtraces when they are directed to
stderr. The output is unchanged.

`printTrace()` goes back to taking only a stream, as its prefixed form
was only used for this path.

Not covered: the fatal error handlers that run after the backtrace are
still handed `std::cerr`, as before.

Risk Level: low
Testing: unit test for `StderrWriter`; the existing
`Backward.FaultToStderr`, `Backward.TraceToStderr` and coroutine stderr
tests cover the output format
Docs Changes: n/a
Release Notes: n/a
Platform Specific Features: n/a

---------

(cherry picked from commit 1e35e9c)

Signed-off-by: Ryan Northey <[email protected]>
phlax added a commit to phlax/envoy that referenced this pull request Nov 18, 2026
…49215)

`ext_proc_http_integration_test` ran with the filter's default message
timeout of 200ms, and a 900ms timeout on the HTTP client, both in real
time. Under tsan a round trip through the fake side stream server can
take longer, the request is then failed with a 504 and never reaches the
upstream, and the test fails waiting for the upstream connection and
crashes on the stream it did not get.

Default both to 5s, as envoyproxy#48964 did for the gRPC fixture. The tests that
rely on the message timeout set it to 200ms: the two timeout tests, and
the two with a cluster that does not exist, where nothing responds.

Seen in CI on envoyproxy#49207 (tsan, `ServerNoRequestHeaderMutation`: `Timed out
waiting for new connection`, then a segmentation fault).

Testing: `*ServerNoRequestHeaderMutation*` with `--config=tsan` on RBE:

| | runs | failed |
|---|---|---|
| main (0dfc464) | 1000 | 2 |
| this change | 3000 | 0 |

Risk Level: low (test only)

Signed-off-by: Ryan Northey <[email protected]>
(cherry picked from commit 14507a6)
Signed-off-by: Ryan Northey <[email protected]>
yanavlasov pushed a commit that referenced this pull request Nov 19, 2026
#49207 introduced a unix write to stderr_writer. This adds a win32
branch to fix compilation on it

Example failure:
https://github.com/curioswitch/py-envoy-server/actions/runs/43867024431/job/133298099621

@yanavlasov BTW will you add daily sync / builds to envoy-windows repo?

Risk Level: low
Testing: none
Docs Changes: 
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]

Signed-off-by: Anuraag Agrawal <[email protected]>
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.

3 participants