mratsim / weave

A state-of-the-art multithreading runtime: message-passing based, fast, scalable, ultra-low overhead
Other
532 stars 22 forks source link

background processing some submits go missing #161

Open HumptyNumpty opened 3 years ago

HumptyNumpty commented 3 years ago

It would appear some submitted jobs are not being processed The same problem occurs if the sleep statement is replaced by a compute bound task - discard sum(toSeq(0 .. 100_000_000)) Silly code below

serving new req 1 new req 2 new req 3 processing job 1 new req 4 new req 5 new req 6 processing job 2 processing job 3 new req 7 processing job 4 processing job 6 processing job 7

Code

import
    asynchttpserver,
    asyncdispatch,
    std/[atomics, os],
    weave

# ctrl-c interupt handling
type EKeyboardInterrupt = object of OSError

proc handler() {.noconv.} =
    raise newException(EKeyboardInterrupt, "Keyboard Interrupt")

setControlCHook(handler)

# GLOBALS
var shutdownWeave: Atomic[bool]
shutdownWeave.store(false, moRelaxed)

var executorThread: Thread[ptr Atomic[bool]]
executorThread.runInBackground(Weave, shutdownWeave.addr)

var reqnum: int

# fn submit to weave
proc display_int(x: int): bool =
    sleep(1000)
    echo "processing job " & $x
    return true

# request callback
proc cb(req: Request) {.async.} =
    inc(reqnum)

    echo "new req " & $reqnum
    let ok = submit display_int(reqnum)

    await req.respond(Http200, "Hello World " & $reqnum)

proc serve() =    
    setupSubmitterThread(Weave)
    waitUntilReady(Weave)

    var server = newAsyncHttpServer()
    waitFor server.serve(Port(8080), cb)

proc cleanup() = 
    echo "\n\rcleaning up"
    echo "shutting down weave"
    shutdownWeave.store(true)

when isMainModule:

    echo "serving"
    try:
        serve()
    except EKeyboardInterrupt:
        cleanup()

    echo "end"