Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in httpclient #14738

Open
zeitbit opened this issue Jun 20, 2020 · 2 comments
Open

Bug in httpclient #14738

zeitbit opened this issue Jun 20, 2020 · 2 comments

Comments

@zeitbit
Copy link

zeitbit commented Jun 20, 2020

Running httpclient compiled in a DLL crashes after the first execution:

Traceback (most recent call last)                                                                                      
C:\Test\lib.nim(7) fnNet   
C:\Nim\lib\pure\httpclient.nim(1074) getContent                                                           
C:\Nim\lib\pure\httpclient.nim(1069) get                                                                  
C:\Nim\lib\pure\httpclient.nim(1045) request                                                              
C:\Nim\lib\pure\httpclient.nim(1029) request                                                              
C:\Nim\lib\pure\httpclient.nim(979) requestAux                                                            
C:\Nim\lib\pure\httpcore.nim(180) hasKey                                                                  
C:\Nim\lib\pure\collections\tables.nim(880) hasKey                                                        
SIGSEGV: Illegal storage access. (Attempt to read from nil?) 

Here is a minimal example to reproduce the crash:

C code (EXE):

#include "windows.h"

typedef void (*DLLPROC)();

int main()
{
    HINSTANCE dll;

    dll = LoadLibrary("lib.dll");

    if (dll)
    {
        DLLPROC fnNet = (DLLPROC) GetProcAddress(dll, "fnNet");

        if (fnNet)
        {
            for (int i = 0; i < 100; i++)
            {
                (*fnNet)();
            }
        }

        FreeLibrary(dll);
    }
}

Nim code (DLL):

import httpclient

proc fnNet*() {.exportc, dynlib.} =
    var httpClient = newHttpClient()

    try:
        discard httpClient.getContent("http://google.com")
        echo "ok"
    except:
        echo getCurrentExceptionMsg()

Without try/except the traceback is:

Traceback (most recent call last)                                                                                      
C:\Test\lib.nim(6) fnNet                                   
C:\Nim\lib\pure\httpclient.nim(1074) getContent                                                           
C:\Nim\lib\pure\httpclient.nim(1069) get                                                                  
C:\Nim\lib\pure\httpclient.nim(1045) request                                                              
C:\Nim\lib\pure\httpclient.nim(1029) request                                                              
C:\Nim\lib\pure\httpclient.nim(1006) requestAux                                                           
C:\Nim\lib\pure\httpclient.nim(814) parseResponse                                                         
C:\Nim\lib\pure\httpcore.nim(158) add                                                                     
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

I tested this with Nim 1.2 and 1.2.2.

@ghost
Copy link

ghost commented Jun 20, 2020

I think you have to initialize the Nim GC in the main C app if you're using refc, although I never did that so I don't remember how :P

@zeitbit
Copy link
Author

zeitbit commented Jun 20, 2020

I think you have to initialize the Nim GC in the main C app if you're using refc, although I never did that so I don't remember how :P

It's not necessary to call NimMain directly. The OS calls DllMain when the DLL is loaded with LoadLibrary, and NimMain is called by DllMain:

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, 
                    LPVOID lpvReserved) {
	if(fwdreason == DLL_PROCESS_ATTACH) {
	NimMain();
}
	return 1;
}

@ghost ghost added the Standard Library label Jul 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant