Skip to content

Conversation

@kbenne
Copy link
Contributor

@kbenne kbenne commented Dec 18, 2025

/**
 * Register a Ruby end_proc hook that tears down ANTLR singletons safely when the interpreter exits.
 *
 * Why does this exist? Linking the Modelica parser into openstudio.so drags in ANTLR's global
 * singletons (notably CommonTokenFactory::DEFAULT). Ruby unloads extensions with dlclose() and runs
 * them through its GC at exit, so ANTLR's destructors fire while the interpreter is already winding
 * down, which consistently segfaults. We cannot delete the singleton from a global static
 * destructor either because every executable that links
 * openstudio_modelica (gtests, CLI, etc.) would explode during __cxa_finalize() before Ruby was
 * ever involved, and we cannot register Ruby callbacks from such a static because rb_set_end_proc
 * must run after the VM boots.
 *
 * Instead, we register a Ruby end_proc callback once per interpreter that releases the singleton
 * *only* when the Ruby VM shuts down. That keeps the workaround scoped to Ruby, preserves the
 * default teardown order for other binaries, and mirrors Ruby's own extension lifecycle.
 */

@anchapin anchapin added the Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge. label Dec 18, 2025
@anchapin anchapin closed this Dec 18, 2025
@anchapin anchapin reopened this Dec 18, 2025
@github-actions
Copy link

🧪 Test Results Dashboard

Summary

Metric Value
Total Tests 4160
Passed 4149
Failed 11
Errors 0
Skipped 0
Success Rate 99.7%
Generated 2025-12-18 22:11:37 UTC

⚠️ Minor Issues Detected

🔍 Failed Tests (11 failures)

Linux-c++ (11 failures)

OpenStudioCLI.Classic.test_measure_manager.OpenStudioCLI.Classic.test_measure_manager (run1)

Error Message:

Failed

Full Details:

No details available
CLITest-UUID_Test-uuid_hash.CLITest-UUID_Test-uuid_hash (run1)

Error Message:

Failed

Full Details:

No details available
CLITest-test_bundle-bundle.CLITest-test_bundle-bundle (run1)

Error Message:

Failed

Full Details:

No details available
CLITest-test_bundle-bundle_git.CLITest-test_bundle-bundle_git (run1)

Error Message:

Failed

Full Details:

No details available
CLITest-test_bundle-bundle_native_embedded.CLITest-test_bundle-bundle_native_embedded (run1)

Error Message:

Failed

Full Details:

No details available
CLITest-test_bundle-bundle.CLITest-test_bundle-bundle (run3)

Error Message:

Failed

Full Details:

No details available
CLITest-test_bundle-bundle_git.CLITest-test_bundle-bundle_git (run3)

Error Message:

Failed

Full Details:

No details available
CLITest-test_bundle-bundle_native_embedded.CLITest-test_bundle-bundle_native_embedded (run3)

Error Message:

Failed

Full Details:

No details available
CLITest-test_bundle-bundle.CLITest-test_bundle-bundle (run2)

Error Message:

Failed

Full Details:

No details available
CLITest-test_bundle-bundle_git.CLITest-test_bundle-bundle_git (run2)

Error Message:

Failed

Full Details:

No details available
CLITest-test_bundle-bundle_native_embedded.CLITest-test_bundle-bundle_native_embedded (run2)

Error Message:

Failed

Full Details:

No details available

📊 Test Run Information

Run XML File Status
run1 results.xml ✅ Found
run3 results.xml ✅ Found
run2 results.xml ✅ Found

@anchapin anchapin merged commit 8baed79 into develop Dec 19, 2025
5 of 7 checks passed
Comment on lines +38 to +39
static std::once_flag registeredFlag;
std::call_once(registeredFlag, []() { rb_set_end_proc(modelica_antlr_cleanup, Qnil); });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yay! more ruby shenanigans.

Anyways, this looks legit, is properly documented, and the code quality is nice.

I'm sure it was such a blast to track down the cause and fix it... good job @kbenne!

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

Labels

Pull Request - Ready for CI This pull request if finalized and is ready for continuous integration verification prior to merge.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants