olivierkes / manuskript

A open-source tool for writers
http://www.theologeek.ch/manuskript
GNU General Public License v3.0
1.74k stars 230 forks source link

Crash when pressing delete in edit box after cutting text out #1321

Open ByteOfBrie opened 3 weeks ago

ByteOfBrie commented 3 weeks ago

I managed to find and reproduce a weird crash. It's hard to determine the exact steps, but I can get it to crash consistently on my system. I can sometimes skip some of these steps and have it crash, but it's less consistent.

  1. With the metadata tab up, edit text in the main editor window
  2. Click and select text in the notes/reference section of the metadata sidebar
  3. Use ctrl-x to cut the text
  4. Wait about 2 seconds
  5. Press delete (not backspace)
  6. Manuskript crashes

The relevant section of log is here:

2024-08-20 23:00:42,521 - manuskript.logging - CRITICAL - An unhandled exception has occurred!
Traceback (most recent call last):
  File "/home/brie/code/manuskript/bin/../manuskript/mainWindow.py", line 765, in documentsDelete
    if self._lastFocus: self._lastFocus.delete()
                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brie/code/manuskript/bin/../manuskript/ui/editors/mainEditor.py", line 274, in delete
    def delete(self): self.currentEditor().delete()
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brie/code/manuskript/bin/../manuskript/ui/editors/editorWidget.py", line 430, in delete
    if self.getCurrentItemView(): self.getCurrentItemView().delete()
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'MDEditView' object has no attribute 'delete'

On further testing, the "first" issue appears to happen right after I cut the text. Very quickly after cutting text from the editor window, the text box loses focus.

If I cut text and then press an arrow key, that input allows me to keep focus on the window, and then everything functions as expected. Likewise, if I press ctrl-x and then delete very quickly, it doesn't crash. It needs a pause between cutting text and the next input to lose focus, and then pressing delete will crash Manuskript.


The crash is the most visible part of this, but losing focus is bad on its own. When that happens, you lose the ability to undo, and you have to click again to edit the text. This is mostly an annoyance given that you have the text on the clipboard, but still enough of a problem that this part should also be fixed (eventually), not (just) a band-aid on the crashing.

TheJackiMonster commented 3 weeks ago

Thanks for debugging the exact cause of this. I'm not sure whether loosing focus after cutting a portion of text is done by the application or Qt itself though. So potentially there's little to solve this part of the issue.

I agree that it would be better to fix that though. Maybe we can simply move the cursor to the start/end of the removed text and reapply focus or something to workaround this.

ByteOfBrie commented 3 weeks ago

I agree that it would be better to fix that though. Maybe we can simply move the cursor to the start/end of the removed text and reapply focus or something to workaround this.

Yeah, exactly. If the cursor manages to stay focused in the text box, it should prevent this issue entirely.

I find it weird that pressing an arrow key quickly enough prevents the loss of focus. If I wait for the loss of focus to occur first, movement keys don't appear to do anything (nor does almost anything else). f2 will make it edit and select the chapter name, which then gives something back to focus on, and then prevents crashing.

I'm not sure if I'll get around to trying to fix this, but I think I'd start with trying to track when it's gaining and losing focus, and seeing if I can find anything interesting there.


While screwing around with this, I've occasionally managed to get into a state where the metadata sidebar gets greyed out and has no data (just like when I select the document root), despite having my cursor in the document editor. If I type enough in the document, it fixes itself. This might be related or might be an entirely separate issue, but the fact that they're happening with similar sorts of actions makes me suspicious enough to mention it here. If I notice any more information about this, I'll open it as a separate issue.

ByteOfBrie commented 2 weeks ago

Possibly unrelated, but pressing ctrl-backspace (to delete a word) in the metadata tab also loses focus on the editing box. It still has focus on something, so it doesn't crash when pressing delete, but it's maybe related (and really annoying).

Edit: I'm not seeing this as consistently as I originally thought.

It doesn't seem to happen at all if I'm editing the metadata from the root, but if I'm editing a scene, then it can happen sometimes. If I click off the box, back on, and then press ctrl-backspace, it always (?) happens, but if I'm editing a particular text box, it seems less likely to happen.

Since it seems like it might matter, the project that I'm seeing the crashes has a bunch of scenes connected directly to the root, no folders.