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

crash when calling req.closeWait() #345

Open
moin110x opened this issue Jan 13, 2023 · 3 comments
Open

crash when calling req.closeWait() #345

moin110x opened this issue Jan 13, 2023 · 3 comments

Comments

@moin110x
Copy link

moin110x commented Jan 13, 2023

Hi,
please forgive me if i misunderstood something really basic that makes this issue;
i am trying to do a simple get request and the application is crashing

proc getipv4*(): string =
    var session = createSession(true)
    # let address = initTAddress("api.ipify.org:443")
    let address = resolveTAddress("api.ipify.org",443.Port,Domain.AF_INET)[0]
    let ha = getAddress(address, HttpClientScheme.Secure, "/")
    var req = HttpClientRequestRef.new(session, ha, MethodGet)
    let response = waitFor fetch(req)
    if response.status == 200:
        let data = cast[string](response.data)
        result = data
    else:
        result = "fail"

    waitFor(req.closeWait()) #crash happens at this line with msg  'Future operation cancelled!'
    waitFor(session.closeWait())

log:

terminate called without an active exception
Traceback (most recent call last)
C:\....\server.nim(68) server
C:\...\nettools.nim(35) getipv4
C:\....\nimble\pkgs\chronos-3.0.11\chronos\asyncloop.nim(1203) waitFor
C:\....\nimble\pkgs\chronos-3.0.11\chronos\asyncloop.nim(295) poll
C:\....\nimble\pkgs\chronos-3.0.11\chronos\asyncsync.nim(372) popFirst
SIGABRT: Abnormal termination.

@cheatfate
Copy link
Collaborator

cheatfate commented Jan 14, 2023

Your source is a bit incomplete, imho.
I have tested this one

import chronos
import chronos/apps/http/httpclient

proc getipv4*(): string =
    var session = HttpSessionRef.new()
    # let address = initTAddress("api.ipify.org:443")
    let address = resolveTAddress("api.ipify.org",443.Port,Domain.AF_INET)[0]
    let ha = getAddress(address, HttpClientScheme.Secure, "/")
    var req = HttpClientRequestRef.new(session, ha, MethodGet)
    let response = waitFor fetch(req)
    if response.status == 200:
        let data = cast[string](response.data)
        result = data
    else:
        result = "fail"

    waitFor(req.closeWait()) #crash happens at this line with msg  'Future operation cancelled!'
    waitFor(session.closeWait())

when isMainModule:
  echo getipv4()

on both Linux and Windows with latest chronos 945c304 and both versions returned

Error: unhandled exception: Could not connect to remote host [HttpConnectionError]

If you modify your source to this

import chronos
import chronos/apps/http/httpclient

proc getipv4*(): string =
    var session = HttpSessionRef.new()
    # let address = initTAddress("api.ipify.org:443")
    let address = session.getAddress("https://api.ipify.org").valueOr:
      echo "Could not resolve address api.ipify.org with error"
      echo error
      return ""
    var req = HttpClientRequestRef.new(session, address, MethodGet)
    let response = waitFor fetch(req)
    if response.status == 200:
        let data = cast[string](response.data)
        result = data
    else:
        result = "fail"

    waitFor(req.closeWait()) #crash happens at this line with msg  'Future operation cancelled!'
    waitFor(session.closeWait())

when isMainModule:
  echo getipv4()

you will be able to see your IP address.

What version of Nim do you use?

@moin110x
Copy link
Author

Thankyou for your answer
i use nim Version 1.6.10
and also i am forced to use the cpp backend (cause of a lib) and therefore i had to modify nim-bearssl c header file (csources/tools/brssl.h) in order to compile the project , otherwise i get undefined refrence linkage error ;
just a extern "C" is added nothing more

@moin110x
Copy link
Author

moin110x commented Jan 14, 2023

after furthur tests , i understood that when i compile with C backend, everything works fine as expected
but with CPP backend, it compiles (with my little modification ofcourse) but i get the exact same exceptions
it's so strange to me, i expect a segfalut or something like it rather than nim exception when i corroupt the underlying C

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

2 participants