Skip to content

Commit

Permalink
Make async stacktraces less verbose (#21091)
Browse files Browse the repository at this point in the history
* Name iterators something human readable

Remove intermediate async procs from stacktraces

Clean async traceback message from reraises message

* Remove unused import/variable

* Fix failing tests

Don't add {.stackTrace: off.} to anonymous procs (They already don't appear in stacktrace)

* Fix failing tests in pragma category

Now check that the nim is a routine type first so we don't run into any assertion defects

* Hide stack trace pragma in docs and update doc tests

User doesn't need to know if something won't appear so this more becomes verbose noise

If this is a bad idea we can always add a `when defined(nimdoc)` switch so we don't add {.stackTrace: off.} to the Future[T] returning proc for docs
  • Loading branch information
ire4ever1190 authored Dec 15, 2022
1 parent 91ce8c3 commit d88f46d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 63 deletions.
4 changes: 3 additions & 1 deletion compiler/renderer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,9 @@ proc isHideable(config: ConfigRef, n: PNode): bool =
# xxx compare `ident` directly with `getIdent(cache, wRaises)`, but
# this requires a `cache`.
case n.kind
of nkExprColonExpr: result = n[0].kind == nkIdent and n[0].ident.s in ["raises", "tags", "extern", "deprecated", "forbids"]
of nkExprColonExpr:
result = n[0].kind == nkIdent and
n[0].ident.s.nimIdentNormalize in ["raises", "tags", "extern", "deprecated", "forbids", "stacktrace"]
of nkIdent: result = n.ident.s in ["gcsafe", "deprecated"]
else: result = false

Expand Down
16 changes: 4 additions & 12 deletions lib/pure/asyncfutures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -329,29 +329,21 @@ proc `$`*(stackTraceEntries: seq[StackTraceEntry]): string =
if leftLen > longestLeft:
longestLeft = leftLen

var indent = 2
# Format the entries.
for entry in entries:
let (filename, procname) = getFilenameProcname(entry)

if procname == "":
if entry.line == reraisedFromBegin:
result.add(spaces(indent) & "#[\n")
indent.inc(2)
elif entry.line == reraisedFromEnd:
indent.dec(2)
result.add(spaces(indent) & "]#\n")
continue
if procname == "" and entry.line == reraisedFromBegin:
break

let left = "$#($#)" % [filename, $entry.line]
result.add((spaces(indent) & "$#$# $#\n") % [
result.add((spaces(2) & "$# $#\n") % [
left,
spaces(longestLeft - left.len + 2),
procname
])
let hint = getHint(entry)
if hint.len > 0:
result.add(spaces(indent+2) & "## " & hint & "\n")
result.add(spaces(4) & "## " & hint & "\n")

proc injectStacktrace[T](future: Future[T]) =
when not defined(release):
Expand Down
16 changes: 10 additions & 6 deletions lib/pure/asyncmacro.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ proc newCallWithLineInfo(fromNode: NimNode; theProc: NimNode, args: varargs[NimN
template createCb(retFutureSym, iteratorNameSym,
strName, identName, futureVarCompletions: untyped) =
bind finished

var nameIterVar = iteratorNameSym
proc identName {.closure.} =
proc identName {.closure, stackTrace: off.} =
try:
if not nameIterVar.finished:
var next = nameIterVar()
Expand Down Expand Up @@ -151,6 +150,10 @@ proc asyncSingleProc(prc: NimNode): NimNode =
result[0][0] = quote do: Future[void]
return result

if prc.kind in RoutineNodes and prc.name.kind != nnkEmpty:
# Only non anonymous functions need/can have stack trace disabled
prc.addPragma(nnkExprColonExpr.newTree(ident"stackTrace", ident"off"))

if prc.kind notin {nnkProcDef, nnkLambda, nnkMethodDef, nnkDo}:
error("Cannot transform this node kind into an async proc." &
" proc/method definition or lambda node expected.", prc)
Expand Down Expand Up @@ -209,7 +212,7 @@ proc asyncSingleProc(prc: NimNode): NimNode =
# -> {.pop.}
# -> <proc_body>
# -> complete(retFuture, result)
var iteratorNameSym = genSym(nskIterator, $prcName & "Iter")
var iteratorNameSym = genSym(nskIterator, $prcName & " (Async)")
var procBody = prc.body.processBody(retFutureSym, futureVarIdents)
# don't do anything with forward bodies (empty)
if procBody.kind != nnkEmpty:
Expand Down Expand Up @@ -251,9 +254,10 @@ proc asyncSingleProc(prc: NimNode): NimNode =
# friendlier stack traces:
var cbName = genSym(nskProc, prcName & NimAsyncContinueSuffix)
var procCb = getAst createCb(retFutureSym, iteratorNameSym,
newStrLitNode(prcName),
cbName,
createFutureVarCompletions(futureVarIdents, nil))
newStrLitNode(prcName),
cbName,
createFutureVarCompletions(futureVarIdents, nil)
)
outerProcBody.add procCb

# -> return retFuture
Expand Down
11 changes: 6 additions & 5 deletions nimdoc/testproject/expected/testproject.html
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ <h1><a class="toc-backref" href="#12">Procs</a></h1>
</div>
<div id="asyncFun1-procs-all">
<div id="asyncFun1">
<dt><pre><span class="Keyword">proc</span> <a href="#asyncFun1"><span class="Identifier">asyncFun1</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">Future</span><span class="Other">[</span><span class="Identifier">int</span><span class="Other">]</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">]</span><span class="Other">,</span>
<dt><pre><span class="Keyword">proc</span> <a href="#asyncFun1"><span class="Identifier">asyncFun1</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">Future</span><span class="Other">[</span><span class="Identifier">int</span><span class="Other">]</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">stackTrace</span><span class="Other">:</span> <span class="DecNumber">false</span><span class="Other">,</span>
<span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">,</span> <span class="Identifier">ValueError</span><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dd>

Expand All @@ -515,8 +516,8 @@ <h1><a class="toc-backref" href="#12">Procs</a></h1>
</div>
<div id="asyncFun2-procs-all">
<div id="asyncFun2">
<dt><pre><span class="Keyword">proc</span> <a href="#asyncFun2"><span class="Identifier">asyncFun2</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">owned</span><span class="Other">(</span><span class="Identifier">Future</span><span class="Other">[</span><span class="Identifier">void</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dt><pre><span class="Keyword">proc</span> <a href="#asyncFun2"><span class="Identifier">asyncFun2</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">owned</span><span class="Other">(</span><span class="Identifier">Future</span><span class="Other">[</span><span class="Identifier">void</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">stackTrace</span><span class="Other">:</span> <span class="DecNumber">false</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dd>


Expand All @@ -527,8 +528,8 @@ <h1><a class="toc-backref" href="#12">Procs</a></h1>
</div>
<div id="asyncFun3-procs-all">
<div id="asyncFun3">
<dt><pre><span class="Keyword">proc</span> <a href="#asyncFun3"><span class="Identifier">asyncFun3</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">owned</span><span class="Other">(</span><span class="Identifier">Future</span><span class="Other">[</span><span class="Identifier">void</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dt><pre><span class="Keyword">proc</span> <a href="#asyncFun3"><span class="Identifier">asyncFun3</span></a><span class="Other">(</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">owned</span><span class="Other">(</span><span class="Identifier">Future</span><span class="Other">[</span><span class="Identifier">void</span><span class="Other">]</span><span class="Other">)</span> {.<span><span class="Other pragmadots">...</span></span><span class="pragmawrap"><span class="Identifier">stackTrace</span><span class="Other">:</span> <span class="DecNumber">false</span><span class="Other">,</span> <span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">Exception</span><span class="Other">]</span><span class="Other">,</span>
<span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Identifier">RootEffect</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">forbids</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span>.}</pre></dt>
<dd>


Expand Down
2 changes: 1 addition & 1 deletion tests/async/tasync_gcunsafe.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
errormsg: "'anotherGCSafeAsyncProcIter' is not GC-safe as it calls 'asyncGCUnsafeProc'"
errormsg: "'anotherGCSafeAsyncProc (Async)' is not GC-safe as it calls 'asyncGCUnsafeProc'"
cmd: "nim c --threads:on $file"
file: "asyncmacro.nim"
"""
Expand Down
47 changes: 9 additions & 38 deletions tests/async/tasync_traceback.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,51 +67,22 @@ import re
const expected = """
b failure
Async traceback:
tasync_traceback\.nim\(\d+?\)\s+?tasync_traceback
asyncmacro\.nim\(\d+?\)\s+?a
asyncmacro\.nim\(\d+?\)\s+?aNimAsyncContinue
## Resumes an async procedure
tasync_traceback\.nim\(\d+?\)\s+?aIter
asyncmacro\.nim\(\d+?\)\s+?b
asyncmacro\.nim\(\d+?\)\s+?bNimAsyncContinue
## Resumes an async procedure
tasync_traceback\.nim\(\d+?\)\s+?bIter
#\[
tasync_traceback\.nim\(\d+?\)\s+?tasync_traceback
asyncmacro\.nim\(\d+?\)\s+?a
asyncmacro\.nim\(\d+?\)\s+?aNimAsyncContinue
## Resumes an async procedure
asyncmacro\.nim\(\d+?\)\s+?aIter
asyncfutures\.nim\(\d+?\)\s+?read
\]#
tasync_traceback\.nim\(\d+?\) tasync_traceback
tasync_traceback\.nim\(\d+?\) a \(Async\)
tasync_traceback\.nim\(\d+?\) b \(Async\)
Exception message: b failure
bar failure
Async traceback:
tasync_traceback\.nim\(\d+?\)\s+?tasync_traceback
asyncdispatch\.nim\(\d+?\)\s+?waitFor
asyncdispatch\.nim\(\d+?\)\s+?poll
tasync_traceback\.nim\(\d+?\) tasync_traceback
asyncdispatch\.nim\(\d+?\) waitFor
asyncdispatch\.nim\(\d+?\) poll
## Processes asynchronous completion events
asyncdispatch\.nim\(\d+?\)\s+?runOnce
asyncdispatch\.nim\(\d+?\)\s+?processPendingCallbacks
asyncdispatch\.nim\(\d+?\) runOnce
asyncdispatch\.nim\(\d+?\) processPendingCallbacks
## Executes pending callbacks
asyncmacro\.nim\(\d+?\)\s+?barNimAsyncContinue
## Resumes an async procedure
tasync_traceback\.nim\(\d+?\)\s+?barIter
#\[
tasync_traceback\.nim\(\d+?\)\s+?tasync_traceback
asyncdispatch\.nim\(\d+?\)\s+?waitFor
asyncdispatch\.nim\(\d+?\)\s+?poll
## Processes asynchronous completion events
asyncdispatch\.nim\(\d+?\)\s+?runOnce
asyncdispatch\.nim\(\d+?\)\s+?processPendingCallbacks
## Executes pending callbacks
asyncmacro\.nim\(\d+?\)\s+?fooNimAsyncContinue
## Resumes an async procedure
asyncmacro\.nim\(\d+?\)\s+?fooIter
asyncfutures\.nim\(\d+?\)\s+?read
\]#
tasync_traceback\.nim\(\d+?\) bar \(Async\)
Exception message: bar failure
"""
Expand Down

0 comments on commit d88f46d

Please sign in to comment.