microbit-foundation / python-editor-v3

Micro:bit Educational Foundation Python Editor V3
https://python.microbit.org
MIT License
56 stars 37 forks source link

[CodeMirror] Invalid position errors due to stale tooltips #1053

Closed microbit-matt-hillsdon closed 1 year ago

microbit-matt-hillsdon commented 1 year ago

We see common traces like this:

RangeError Invalid position 125 in document of length 122 numbers vary though this one is most common

Crashed in non-app: ../node_modules/@codemirror/state/dist/index.js in lineAt
<anonymous> in Array.map
Called from: ../node_modules/@codemirror/view/dist/index.js in read
<anonymous> in Array.map
Called from: ../node_modules/@codemirror/view/dist/index.js in measure
../../../../browser/src/helpers.ts in i at line 73:23

This amounts to a crash in the tooltip module.

I've managed to reproduce it by inserting a significant delay in the removal of a signature help tooltip on the last line of the document and then deleting the line.

We should review all asynchronous tooltip handling.

Detailed repro:

# Imports go at the top
from microbit import *

# Code in a 'while True:' loop repeats forever
while True:
    display.show(Image.HEART)
    sleep(1000)
    display.scroll()

I suspect there are other repros, but the general idea of it being a tooltip for an invalid position due to async is likely common to them.

Pretty trace from a repro:

react_devtools_backend.js:4026 RangeError: Invalid position 173 in document of length 153
    at TextLeaf.lineAt (index.js:13:1)
    at EditorView.coordsAtPos (index.js:6678:1)
    at index.js:8203:1
    at Array.map (<anonymous>)
    at Object.readMeasure (index.js:8201:1)
    at index.js:6380:1
    at Array.map (<anonymous>)
    at EditorView.measure (index.js:6378:1)
    at index.js:6504:1
microbit-matt-hillsdon commented 1 year ago

lint and signature help have this characteristic, but lint maps errors through document changes so I guess would move the error to the end of the document (and then it'd be replaced by a diagnostics update). So I think we need to do something similar with the position we're intending to show signature help for.

Note: 125, which is the common position for this error is the cursor position in the brackets of a display.show in the default while loop, so that makes a lot of sense.

microbit-matt-hillsdon commented 1 year ago

This is likely fixed by https://github.com/microbit-foundation/python-editor-v3/pull/1054 but leaving open for a bit to monitor impact on reported errors on the beta.

microbit-matt-hillsdon commented 1 year ago

No further incidence on staging so resolving.