Skip to content

Async try transformation fails on finally #2650

@Beluki

Description

@Beluki

To learn a bit more about Nim, I wrote a small script that checks website links:

import asyncdispatch
import httpclient
import htmlparser
import strtabs
import streams
import strutils
import xmltree

var running = 0

proc testLink(uri: string) {.async.} =  
    try:
        running += 1

        let client = newAsyncHttpClient()
        let response = await client.get(uri)

        if response.status.startsWith("200"):
            echo("OK: ", uri)
        else:
            echo("FAILED: ", uri)

    except:
        echo("ERROR: ", uri)

    finally:
        running -= 1
        client.close()

proc testLinks(uri: string) =
    let data = getContent(uri)
    let html = parseHTML(newStringStream(data))

    for a in html.findAll("a"):
        let href = a.attrs["href"]

        if not href.isNil and href.startsWith("http"):
            asyncCheck testLink(href)


testLinks("http://www.reddit.com/")

while running != 0:
    poll()

Sometimes, it works. Most of the time it crashes with the following stack trace (or something similar depending on how long it takes for it to crash):

Traceback (most recent call last)
test.nim(46)             test
asyncdispatch.nim(406)   poll
system.nim(2266)         :anonymous
asyncdispatch.nim(206)   fail
asyncdispatch.nim(1211)  cb
asyncdispatch.nim(206)   fail
asyncdispatch.nim(1211)  cb
asyncdispatch.nim(206)   fail
asyncdispatch.nim(1211)  cb
asyncdispatch.nim(206)   fail
asyncdispatch.nim(1211)  cb
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Compiled with: nim -d:ssl compile test.nim
Nim version: Nim Compiler Version 0.11.0 (2015-04-30) [Windows: i386]
GCC from mingw-w64: gcc (GCC) 4.8.3 32 bit

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions