Skip to content

Fix crash when an echo export is followed by another -o output - #7161

Merged
t-paul merged 1 commit into
openscad:masterfrom
thalisantunes:fix-multiple-output-echo-crash
Oct 31, 2026
Merged

t-paul merged 1 commit into
openscad:masterfrom
thalisantunes:fix-multiple-output-echo-crash

Conversation

@thalisantunes

Copy link
Copy Markdown
Contributor

Title: Fix crash when an echo export is followed by another -o output

openscad -o y.echo -o y.stl y.scad terminates with a segmentation fault while the second output is being exported. The same options in the other order (-o y.stl -o y.echo) work. I reproduced it with the 2026.01.02 nightly, and a backtrace of the crash ends in Echostream::output(), called through PRINT_NOCACHE() from RenderStatistic::printAll() of the second export.

Root cause

In src/openscad.cc, Echostream registers Echostream::output with set_output_handler() and passes itself as the user data. The object lives in cmdline() and is destroyed when that call returns, but its destructor never unregistered the handler. With more than one -o, the next cmdline() call prints its messages through a handler that points at the destroyed object, which writes to an std::ofstream that no longer exists.

Fix

Unregister the handler in Echostream::~Echostream(), so that messages go back to stderr once the echo export is done.

Test

Added multiple-outputs_echo-stl_run and multiple-outputs_echo-stl_check to tests/CMakeLists.txt, in the style of the existing relative-output_* tests. The first one runs openscad cube-tests.scad -o multiple-outputs.echo -o multiple-outputs.stl, the second checks that the STL was written.

Build: Ninja, Release, -DEXPERIMENTAL=ON -DHEADLESS=ON -DENABLE_GLX=OFF -DENABLE_CAIRO=OFF -DUSE_BUILTIN_OPENCSG=ON on Ubuntu 26.04 (headless build, EGL offscreen).

  • Without the change (branch with src/openscad.cc reverted): multiple-outputs_echo-stl_run fails with ***Exception: SegFault.
  • With the change: both new tests pass.
  • Full ctest -j16 on master: 2716 tests, 6 failed. With the change: 2718 tests, the same 6 failed and nothing new. The 6 are the export-pdf* tests (export-pdf_centered, export-pdf-fill_*, export-pdf_utf8-☠-2D), which fail on master in this environment as well, so they are unrelated to this change.

Fixes #5614

Echostream registers itself as the global message handler and passes
itself as the user data. It only lives for the duration of one
cmdline() call, but its destructor never unregistered the handler.
With several -o options, the next export printed its messages through
a handler pointing at the destroyed object, so that for example

  openscad -o y.echo -o y.stl y.scad

crashed with a segmentation fault.

Unregister the handler in the destructor, and add a test that runs an
echo export followed by an STL export.

Fixes openscad#5614
@t-paul

t-paul commented Oct 31, 2026

Copy link
Copy Markdown
Member

There's a PR #7127 for solving a much wider issue which should fix this one too.

I tend to think it might be useful to get this small fix in first anyway. What do you think @jordanbrown0?

@jordanbrown0

Copy link
Copy Markdown
Contributor

I haven't reviewed this one yet - need to leave momentarily, will check later today - but by all means apply a simple fix now.

Indeed, my bigger rework makes significant changes in this area, but it's also much higher risk and I haven't decided whether it should go into the current release.

@thalisantunes
thalisantunes marked this pull request as ready for review October 31, 2026 19:04
@thalisantunes

Copy link
Copy Markdown
Contributor Author

Thanks, both. Agreed on keeping this one minimal: it only detaches the output handler before the stream closes, so it should not get in the way of #7127. Happy to rebase if that lands first.

@t-paul
t-paul merged commit f4c369e into openscad:master Oct 31, 2026
9 checks passed
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.

Segmentation fault with "-o something.echo -o something.stl"

3 participants