MATLAB/Simulink freezes at the end of simulation with custom s-function

2 次查看(过去 30 天)
I am in the process of writing an s-function around an external library. The s-function works as expected during the simulation but Matlab and Simulink become non responsive right at the end. It does not give me any crash report or other information. The only action I can take at that point is to kill Matlab process.
The external library I am using uses a multithreading library if that is of any help (https://github.com/mitsuba-renderer/nanothread). Outside of Matlab/Simulink environment, this library works perfectly. I am also setting the library to use a single thread.
I can see that the mdlTerminate function is called as expected. In it, I delete all the objects I created and shut down all the contexts this external library requires.
Does anyone has any suggestion how to debug this issue?

回答(1 个)

Ruchika Parag
Ruchika Parag 2025-7-9
Hi @Leonardo, it appears you're running into a known behavior where MATLAB/Simulink freezes at the end of a simulation with a custom S‑Function that uses an external multithreaded library. Since traditional MATLAB commands can't debug this, here are some strategies to debug the issue without relying on non‑MATLAB tools:
1. Attach a Debugger After the Hang
Run your model under the MATLAB IDE, and when Simulink hangs:
  • Open a console and run:
gdb -p <MATLAB_PID>
Once attached, inspect threads with:
info threads
thread apply all bt
  • This shows the current call stack of all threads—useful to see if any are blocked in library cleanup or waiting on a mutex.
2. Check for Threading Deadlocks
Even if you force single-thread mode, your library may still spawn internal threads internally. A deadlock during termination (e.g., joining threads or cleaning shared resources) would cause MATLAB to hang silently.
  • Ensure all threads are properly joined.
  • Ensure all mutexes, locks, or semaphores are released before mdlTerminate completes.
3. Enable Diagnostic Print Statements
Insert debugging prints in mdlStart, mdlOutputs, and mdlTerminate to log important steps. Use ssPrintf(...) for better Simulink compatibility:
ssPrintf("Entering mdlTerminate, stopping nanothread context...\n");
ssPrintf("nanothread context shut down, exiting mdlTerminate...\n");
This helps identify where the hang occurs.
4. Test Outside MATLAB
Create a standalone test executable that initializes and terminates your library just as the S-function does. Run it repeatedly in a loop—even crashing once via simulated library failure—to check if it leaks resources or hangs on shutdown. This isolates whether the issue is library-specific or MATLAB-specific.
5. Try SS_OPTION_EXCEPTION_FREE_CODE
On some platforms, enabling exception-safe code in the S-function helps avoid deadlocks or crashes during cleanup. In your mdlInitializeSizes, apply:
ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
This might prevent unexpected behavior during teardown. Hope this helps!

产品


版本

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by