status-im / nim-chronos

Chronos - An efficient library for asynchronous programming
https://status-im.github.io/nim-chronos/docs/chronos
Apache License 2.0
353 stars 51 forks source link

crash when calling req.closeWait() #345

Open moin110x opened 1 year ago

moin110x commented 1 year ago

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 commented 1 year ago

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 https://github.com/status-im/nim-chronos/commit/945c304197a0e34b1126cb073df64d2df7ce57dd 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 commented 1 year ago

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 commented 1 year ago

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