Skip to content

Commit 4293a45

Browse files
authored
Harden skip in Scanner (#21607)
After running metals for a while, I sometimes see a rogue java process at 100% even after I closed down sbt and vscode. With jstack I got the following stack trace: java.lang.Thread.State: RUNNABLE at dotty.tools.dotc.parsing.Scanners$Scanner.handleNewLine(Scanners.scala:613) at dotty.tools.dotc.parsing.Scanners$Scanner.nextToken(Scanners.scala:396) at dotty.tools.dotc.parsing.Scanners$Scanner.skip(Scanners.scala:312) at dotty.tools.dotc.parsing.Parsers$Parser.skip(Parsers.scala:280) at dotty.tools.dotc.parsing.Parsers$Parser.recur$2(Parsers.scala:376) at dotty.tools.dotc.parsing.Parsers$Parser.statSepOrEnd(Parsers.scala:380) It could be that the loop in skip gives two alternate offsets that would not bump the progress counter. I changed the loop so that it catches infinite looping conditions more robustly.
2 parents 8068239 + b1235b9 commit 4293a45

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

+7-3
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,15 @@ object Scanners {
306306
println(s"\nSTART SKIP AT ${sourcePos().line + 1}, $this in $currentRegion")
307307
var noProgress = 0
308308
// Defensive measure to ensure we always get out of the following while loop
309-
// even if source file is weirly formatted (i.e. we never reach EOF
309+
// even if source file is weirly formatted (i.e. we never reach EOF)
310+
var prevOffset = offset
310311
while !atStop && noProgress < 3 do
311-
val prevOffset = offset
312312
nextToken()
313-
if offset == prevOffset then noProgress += 1 else noProgress = 0
313+
if offset <= prevOffset then
314+
noProgress += 1
315+
else
316+
prevOffset = offset
317+
noProgress = 0
314318
if debugTokenStream then
315319
println(s"\nSTOP SKIP AT ${sourcePos().line + 1}, $this in $currentRegion")
316320
if token == OUTDENT then dropUntil(_.isInstanceOf[Indented])

0 commit comments

Comments
 (0)