Skip to content

Conversation

@anutosh491
Copy link
Collaborator

Description

@JohanMabille and I were discussing that xeus-cpp might not be respecting how execution works in silent mode.

To confirm this I forced config.silent as true from the xeus side. Probably the statement config.silent = true can be added at the top of the execute_request_impl to verify this.

In silent mode on upstream I see the kernel segfaults.
image

[I 2025-11-10 17:44:50.549 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (1/5), keep random ports
[W 2025-11-10 17:44:50.550 ServerApp] kernel ab79cda1-9da9-4e75-b8fc-0a23c4149cd5 restarted
[I 2025-11-10 17:44:50.555 ServerApp] Starting buffering for ab79cda1-9da9-4e75-b8fc-0a23c4149cd5:29f15f55-1399-4317-b8c0-daece3677b2c
[I 2025-11-10 17:44:50.583 ServerApp] Connecting to kernel ab79cda1-9da9-4e75-b8fc-0a23c4149cd5.
[I 2025-11-10 17:44:50.584 ServerApp] Restoring connection for ab79cda1-9da9-4e75-b8fc-0a23c4149cd5:29f15f55-1399-4317-b8c0-daece3677b2c
clang version 20.1.7
Target: arm64-apple-darwin24.5.0
Thread model: posix
InstalledDir: /Users/anutosh491/micromamba/envs/xeus-cpp/bin
 "/Users/anutosh491/micromamba/envs/xeus-cpp/bin/xcpp" -cc1 -triple arm64-apple-macosx15.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name "<<< inputs >>>" -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -funwind-tables=1 -target-sdk-version=15.2 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -fdefine-target-os-macros -fno-modulemap-allow-subdirectory-search -target-cpu apple-m1 -target-feature +zcm -target-feature +zcz -target-feature +v8.4a -target-feature +aes -target-feature +altnzcv -target-feature +ccdp -target-feature +ccpp -target-feature +complxnum -target-feature +crc -target-feature +dotprod -target-feature +flagm -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +fptoint -target-feature +fullfp16 -target-feature +jsconv -target-feature +lse -target-feature +neon -target-feature +pauth -target-feature +perfmon -target-feature +predres -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sb -target-feature +sha2 -target-feature +sha3 -target-feature +specrestrict -target-feature +ssbs -target-abi darwinpcs -debugger-tuning=lldb -fdebug-compilation-dir=/Users/anutosh491/work/xeus-cpp/build -target-linker-version 951.9 -v -fcoverage-compilation-dir=/Users/anutosh491/work/xeus-cpp/build -resource-dir /Users/anutosh491/micromamba/envs/xeus-cpp/lib/clang/20 -isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 -isystem /Library/Developer/CommandLineTools/usr/lib/clang/16/include -isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -isystem /Library/Developer/CommandLineTools/usr/include -isystem "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)" -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I /Users/anutosh491/micromamba/envs/xeus-cpp/include -internal-isystem /Users/anutosh491/micromamba/envs/xeus-cpp/bin/../include/c++/v1 -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Users/anutosh491/micromamba/envs/xeus-cpp/lib/clang/20/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -std=c++23 -fdeprecated-macro -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -fincremental-extensions -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o - -x c++ "<<< inputs >>>"
clang -cc1 version 20.1.7 based upon LLVM 20.1.7 default target arm64-apple-darwin24.5.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Users/anutosh491/micromamba/envs/xeus-cpp/lib/clang/20/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/SubFrameworks"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks"
ignoring duplicate directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /Users/anutosh491/micromamba/envs/xeus-cpp/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1
 /Library/Developer/CommandLineTools/usr/lib/clang/16/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/usr/include
 /Users/anutosh491/micromamba/envs/xeus-cpp/bin/../include/c++/v1
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
Error: signal 11:
0   libxeus-cpp.0.8.0.dylib             0x000000010465a41c _ZN4xcpp7handlerEi + 48
1   libsystem_platform.dylib            0x000000018fc80624 _sigtramp + 56
2   libc++.1.0.dylib                    0x0000000104a6e2c0 _ZNSt3__113basic_ostreamIcNS_11char_traitsIcEEE5flushEv + 120
3   libc++.1.0.dylib                    0x0000000104a712ac _ZNSt3__113basic_ostreamIcNS_11char_traitsIcEEE6sentryC1ERS3_ + 64
4   libxeus-cpp.0.8.0.dylib             0x000000010462e2b8 _ZNSt3__124__put_character_sequenceB7v160006IcNS_11char_traitsIcEEEERNS_13basic_ostreamIT_T0_EES7_PKS4_m + 68
5   libxeus-cpp.0.8.0.dylib             0x000000010464a700 _ZN4xcpp11interpreter20execute_request_implENSt3__18functionIFvN8nlohmann16json_abi_v3_12_010basic_jsonINS1_3mapENS1_6vectorENS1_12basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEbxydSB_NS4_14adl_serializerENS7_IhNSB_IhEEEEvEEEEEiRKSD_N4xeus22execute_request_configESH_ + 524
6   libxeus.13.2.0.dylib                0x000000010476e0ec _ZN4xeus12xinterpreter15execute_requestENS_16xrequest_contextENSt3__18functionIFvN8nlohmann16json_abi_v3_12_010basic_jsonINS2_3mapENS2_6vectorENS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEbxydSC_NS5_14adl_serializerENS8_IhNSC_IhEEEEvEEEEERKSE_NS_22execute_request_configESI_ + 580
7   libxeus.13.2.0.dylib                0x000000010477f4bc _ZN4xeus12xkernel_core15execute_requestENS_8xmessageENS_7channelE + 892
8   libxeus.13.2.0.dylib                0x00000001047820a4 _ZN4xeus12xkernel_core8dispatchENS_8xmessageENS_7channelE + 616
9   libxeus.13.2.0.dylib                0x00000001047816c4 _ZN4xeus12xkernel_core14dispatch_shellENS_8xmessageE + 136

Problem

The temporary xnull object used when config.silent = true was destroyed at the end of the if-block, leaving std::cout and std::cerr with dangling streambuf pointers. This caused signal 11 (segmentation fault) on any subsequent flush or write.

Solution

Prevent segmentation fault in silent execution mode by managing the lifetime of the xnull stream buffer with a std::unique_ptr. Now works as expected

image

Type of change

Please tick all options which are relevant.

  • Bug fix
  • New feature
  • Added/removed dependencies
  • Required documentation updates

@codecov-commenter
Copy link

codecov-commenter commented Nov 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.65%. Comparing base (5860e95) to head (a4ecd70).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #407      +/-   ##
==========================================
+ Coverage   82.05%   82.65%   +0.60%     
==========================================
  Files          21       21              
  Lines         858      859       +1     
  Branches       89       89              
==========================================
+ Hits          704      710       +6     
+ Misses        154      149       -5     
Files with missing lines Coverage Δ
src/xinterpreter.cpp 93.93% <100.00%> (+3.08%) ⬆️
Files with missing lines Coverage Δ
src/xinterpreter.cpp 93.93% <100.00%> (+3.08%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

auto cout_strbuf = std::cout.rdbuf();
auto cerr_strbuf = std::cerr.rdbuf();

std::unique_ptr<xnull> nullbuf;
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: no header providing "std::unique_ptr" is directly included [misc-include-cleaner]

src/xinterpreter.cpp:21:

- #ifndef EMSCRIPTEN
+ #include <memory>
+ #ifndef EMSCRIPTEN

auto null = xnull();
std::cout.rdbuf(&null);
std::cerr.rdbuf(&null);
nullbuf = std::make_unique<xnull>();
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: no header providing "std::make_unique" is directly included [misc-include-cleaner]

            nullbuf = std::make_unique<xnull>();
                           ^

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can always allocate xnull before checking for the config.silent property and redirect only if this is needed. That would avoid a dynamic allocation here. Also we could take the opportunity of fixing this issue to write a small RAII object to perform the redirection.

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