dom96 / jester

A sinatra-like web framework for Nim.
MIT License
1.56k stars 120 forks source link

finally block getting called twice? #301

Closed sdmcallister closed 1 year ago

sdmcallister commented 1 year ago

In my terminal I see the following, but I would expect finally block to only be called once.

DEBUG GET /
trying
exception caught
finally block
finally block

MWE:

import jester

routes:
  get "/":
    try:
      echo "trying"
      raise
      resp "never seen"
    except:
      echo "exception caught"
      resp "exception block"
    finally:
      echo "finally block"
dom96 commented 1 year ago

Can you try this outside jester (for example in an async proc) and see if it's the same? Just to ensure this is a jester-specific issue

sdmcallister commented 1 year ago

@dom96 Thanks!

If you mean some thing like what is below, the following code works as expected with only 1 finally block.

import asyncdispatch

proc tryFinally(){.async.} =
  try:
    echo "trying"
    raise
  except:
    echo "exception caught"
  finally:
    echo "finally block"

waitFor tryFinally()
dom96 commented 1 year ago

Looks like this is indeed a Nim bug. Jester uses break to break out of routes, adding a break in the except block reproduces this.

import asyncdispatch

proc tryFinally(){.async.} =
  block route:
    try:
      echo "trying"
      raise
    except:
      echo "exception caught"
      break route
    finally:
      echo "finally block"