Generally, if I run ngSpice_Circ with some "bad" netlist, it will print error I can then parse. It can also hang indefinitely if I use very long tran interval and very small step (in this case CPU is under 100% load), this is understandable and OK.
However in following case ngSpice_Circ hangs, but CPU remains idle (0% load):
// gcc -o test test.c -lngspice -lpthread -Wall -ggdb
#include <stdio.h>
#include <stdbool.h>
#include <ngspice/sharedspice.h>
int main() {
ngSpice_Init(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
char *netlist[] = {
"* first line is ignored",
".subckt MYSUB 1 2 3 4 PARAMS: r1=68k r2=4k7",
"RR1 1 2 {r1}",
"RR2 3 4 {r2}",
".ends",
"xX1 1 2 3 4 MYSUB",
".tran 100u 20m 0",
".print tran v(2)",
".end"
};
printf("AAA\n");
ngSpice_Circ(netlist);
printf("BBB\n");
return 0;
}
The above example prints AAA, but will not print BBB and just waits. CPU is at 0 load (so it is not stuck in infinite loop). ngspice 36
In my view this is not a bug, but expected behavior. You have sent a netlist to ngspice. It then waits for the command to starting the simulation (e.g. bg_run).
What else should it do?
The culprit is the line
ngSpice_Init(NULL, NULL, NULL, NULL, NULL, NULL, NULL);
ngspice.dll expected a callback function for
ControlledExit*
to notify the caller about an unrecoverable error.I will add some code to skip this callback when
ControlledExit*
is not defined.Last edit: Holger Vogt 2023-06-16
It has to be something else, when I add controlled exit callback the myControlledExit is not called. BBB is also not printed so I can't send more commands.
I have compiled your netlist given on top with MSVC.
After adding in sharedspice.c
at two places, ngSpice_Circ() returns, AAA and BBB are printed.
Could you check your problem with a debugger (gdb ...)?
I downloaded ngspice src (via apt-src install ngspice), I did not applied any patches and compiled it as is with: (./autogen.sh; ./configure --with-ngshared --enable-debug; make), I also haven't made your change yet, and it passes as is (prints both AAA and BBB).
Then I studied ruleset in made a quick guess of what parameters debian uses by default and arrived at these parameters: ./configure --enable-cider --enable-maintainer-mode --enable-openmp --enable-pss --enable-xspice --disable-adms --with-editline=yes --with-x --with-ngshared --enable-debug, recompiled it and it also passes.
Original ngspice lib does not have debug symbols but when I run it in gdb and after it freezes press ctrl+c it shows:
When I then do "bt" it shows:
Not sure if it is useful for something.
So the error only happens when I use distribution provided libngspice (Ubuntu 22.04.2 LTS)
Please have a look at chapter '19.4.1.3 Loading as a string array' of the ngspice manual. It is clearly stated that the last entry of the netlist array has to be NULL.
It is also clearly said in chapter '19.3.2 Exported functions' of the manual that
ControlledExit*
is required.So the behavior you have shown is not a bug, but a user error. Your application should check that the required conditiuons are fulfilled.
It has to be something with the way it was compilled, since recompiled version works you can close it.