Menu

#638 ngSpice_Circ() never ends if subcircuit does not have all parameters specified

v1.0 (example)
closed-works-for-me
nobody
None
5
2023-06-17
2023-06-16
simpletk
No

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

Discussion

  • Holger Vogt

    Holger Vogt - 2023-06-16

    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?

     
  • Holger Vogt

    Holger Vogt - 2023-06-16

    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
  • simpletk

    simpletk - 2023-06-17

    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.

    // gcc -o test test.c -lngspice -lpthread -Wall -ggdb
    
    #include <stdio.h>
    #include <stdbool.h>
    #include <ngspice/sharedspice.h>
    
    int myControlledExit(int exitstatus, bool immediate, bool quitexit, int ident, void *userdata) {
        printf("simple_ngspice_init_exit: es=%d im=%d qe=%d\n", exitstatus, immediate, quitexit);
        return exitstatus;
    }
    
    int main() {
        ngSpice_Init(NULL, NULL, myControlledExit, 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;
    }               
    
     
  • Holger Vogt

    Holger Vogt - 2023-06-17

    I have compiled your netlist given on top with MSVC.
    After adding in sharedspice.c

    if(ngexit)
        ngexit(...)
    

    at two places, ngSpice_Circ() returns, AAA and BBB are printed.

    Could you check your problem with a debugger (gdb ...)?

     
  • simpletk

    simpletk - 2023-06-17

    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:

    Program received signal SIGINT, Interrupt.
    0x00007ffff7514992 in __GI___libc_read (fd=0, buf=0x555555593650, nbytes=1024) at ../sysdeps/unix/sysv/linux/read.c:26
    26      ../sysdeps/unix/sysv/linux/read.c: No such file or directory.
    

    When I then do "bt" it shows:

    #0  0x00007ffff7514992 in __GI___libc_read (fd=0, buf=0x555555593650, nbytes=1024) at ../sysdeps/unix/sysv/linux/read.c:26
    #1  0x00007ffff748ccb6 in _IO_new_file_underflow (fp=0x7ffff7619aa0 <_IO_2_1_stdin_>) at ./libio/libioP.h:947
    #2  0x00007ffff748de16 in __GI__IO_default_uflow (fp=0x7ffff7619aa0 <_IO_2_1_stdin_>) at ./libio/libioP.h:947
    #3  0x00007ffff7d83c09 in ?? () from /lib/x86_64-linux-gnu/libngspice.so.0
    #4  0x00007ffff79176f4 in ?? () from /lib/x86_64-linux-gnu/libngspice.so.0
    #5  0x00007ffff78dc2a7 in ?? () from /lib/x86_64-linux-gnu/libngspice.so.0
    #6  0x00007ffff78dd9d8 in ?? () from /lib/x86_64-linux-gnu/libngspice.so.0
    #7  0x00007ffff78b385c in ngSpice_Circ () from /lib/x86_64-linux-gnu/libngspice.so.0
    #8  0x00005555555552fd in main () at test.c:30
    

    Not sure if it is useful for something.

    So the error only happens when I use distribution provided libngspice (Ubuntu 22.04.2 LTS)

     
  • Holger Vogt

    Holger Vogt - 2023-06-17

    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.

     
  • simpletk

    simpletk - 2023-06-17
    • I've added exit
    • I've added NULL
    • still freezes
    // gcc -o test test.c -lngspice -lpthread -Wall -ggdb
    
    #include <stdio.h>
    #include <stdbool.h>
    #include <ngspice/sharedspice.h>
    
    int myControlledExit(int exitstatus, bool immediate, bool quitexit, int ident, void *userdata) {
        printf("simple_ngspice_init_exit: es=%d im=%d qe=%d\n", exitstatus, immediate, quitexit);
        return exitstatus;
    }
    
    int main() {
        ngSpice_Init(NULL, NULL, myControlledExit, 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",
            NULL
        };
        printf("AAA\n");
        ngSpice_Circ(netlist);
        printf("BBB\n");
        return 0;
    }                           
    
     
  • simpletk

    simpletk - 2023-06-17

    It has to be something with the way it was compilled, since recompiled version works you can close it.

     
  • Holger Vogt

    Holger Vogt - 2023-06-17
    • status: open --> closed-works-for-me
     

Log in to post a comment.