Bug 487544

Summary: Saving a TIFF file, then closing and reopening Krita 5.2.2+, causes a crash when it tries to load the TIFF file in the file preview window
Product: [Applications] krita Reporter: foodrickme
Component: File formatsAssignee: Krita Bugs <krita-bugs-null>
Status: RESOLVED FIXED    
Severity: crash CC: foodrickme, penguinflyer2222
Priority: NOR Keywords: regression
Version: git master (please specify the git hash!)   
Target Milestone: ---   
Platform: macOS (DMG)   
OS: macOS   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: Bug report logs

Description foodrickme 2024-05-25 19:08:38 UTC
Created attachment 169830 [details]
Bug report logs

SUMMARY
When I create a new file as a TIFF file, then close and reopen Krita 5.2.3+ Prealpha (krita-5.2.3-prealpha_06e66b6.dmg), Krita will try to load the TIFF file in the file preview before crashing.

STEPS TO REPRODUCE
1. Create a new file.
2. Save that file as a TIFF file.
3. Close Krita.
4. Reopen Krita.
5. Crash!

OBSERVED RESULT
Krita crashes when I reopen it after saving a TIFF file.

EXPECTED RESULT
Krita should properly process the TIFF file as any other file.

SOFTWARE/OS VERSIONS
macOS: MacBook Air M2, 2022, Sonoma 14.4.1

ADDITIONAL INFORMATION
As a temporary workaround, before closing Krita I can make it "forget" the TIFF file so it doesn't crash when reopening. Alternatively, opening the stable version of Krita and making it forget also works.

I believe this is also a TIFF issue because Krita'll render files sequentially based on when they were saved. When I had saved more non-TIFF files after saving a TIFF file, Krita will crash once it reaches the TIFF file.
Comment 1 Freya Lupen 2024-06-04 17:48:36 UTC
I can confirm this on 5.3.0-prealpha, and that it doesn't happen on 5.2.2.
The backtrace indicates it's a macOS-related bug, and I confirmed it doesn't happen on Linux.
> 2024-06-04 11:31:00.477 krita[73124:6236948] *** Assertion failure in -[QCocoaNSMenu _lockForMainMenuItemArray], NSMenu.m:1117
> *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'API misuse: modification of a menu's items on a non-main thread when the menu is part of the main menu. Main menu contents may only be modified from the main thread.'
> *** First throw call stack:
> (
> 	0   CoreFoundation                      0x000000019fd5b2ec __exceptionPreprocess + 176
> 	1   libobjc.A.dylib                     0x000000019f842788 objc_exception_throw + 60
> 	2   Foundation                          0x00000001a0ece42c -[NSCalendarDate initWithCoder:] + 0
> 	3   AppKit                              0x00000001a35363dc -[NSMenu itemArray] + 32
> 	4   libqcocoa.dylib                     0x000000010320b218 qt_plugin_instance + 221628
> 	5   QtWidgets                           0x0000000106fc9858 _ZN5QMenu11actionEventEP12QActionEvent + 1184
> 	6   QtWidgets                           0x0000000106e9e6bc _ZN7QWidget5eventEP6QEvent + 128
> 	7   QtWidgets                           0x0000000106fc76a0 _ZN5QMenu5eventEP6QEvent + 1176
> 	8   QtWidgets                           0x0000000106e6650c _ZN19QApplicationPrivate13notify_helperEP7QObjectP6QEvent + 292
> 	9   QtWidgets                           0x0000000106e67a24 _ZN12QApplication6notifyEP7QObjectP6QEvent + 508
> 	10  libkritaui.20.0.0.dylib             0x000000010451e5f0 _ZN14KisApplication6notifyEP7QObjectP6QEvent + 240
> 	11  QtCore                              0x0000000107aee29c _ZN16QCoreApplication15notifyInternal2EP7QObjectP6QEvent + 292
> 	12  QtWidgets                           0x0000000106e5aa10 _ZN14QActionPrivate15sendDataChangedEv + 88
> 	13  libkritaui.20.0.0.dylib             0x000000010455fb68 _ZN13KisMainWindow12setReadWriteEb + 32
> 	14  libkritaui.20.0.0.dylib             0x0000000104534954 _ZN11KisDocument12setReadWriteEb + 196
> 	15  libkritaui.20.0.0.dylib             0x00000001045354d0 _ZN11KisDocument8openPathERK7QString6QFlagsINS_8OpenFlagEE + 1324
> 	16  libkritaui.20.0.0.dylib             0x0000000104460120 _ZN18KisFileIconCreator14createFileIconE7QStringR5QIcond5QSize + 1104
> 	17  libkritaui.20.0.0.dylib             0x0000000104462310 _ZN12_GLOBAL__N_111getFileIconENS_21GetFileIconParametersE + 128
> 	18  libkritaui.20.0.0.dylib             0x000000010446359c _ZN12QtConcurrent18StoredFunctorCall1IN12_GLOBAL__N_115IconFetchResultEPFS2_NS1_21GetFileIconParametersEES3_E10runFunctorEv + 64
> 	19  libkritaui.20.0.0.dylib             0x0000000104463364 _ZN12QtConcurrent15RunFunctionTaskIN12_GLOBAL__N_115IconFetchResultEE3runEv + 80
> 	20  QtCore                              0x0000000107942ea4 _ZN10QSemaphore10tryAcquireEii + 676
> 	21  QtCore                              0x000000010793eb08 _ZN7QThread11qt_metacallEN11QMetaObject4CallEiPPv + 1296
> 	22  libsystem_pthread.dylib             0x000000019fc06f94 _pthread_start + 136
> 	23  libsystem_pthread.dylib             0x000000019fc01d34 thread_start + 8
> )
> libc++abi: terminating due to uncaught exception of type NSException

What's happening is, TIFF (and JPEGXL) image thumbnails are created by opening a temporary document. When they do that, they change the enabled state of the Save/Open menu items (in KisMainWindow::setReadWrite). Apparently, macOS's API doesn't allow modification of main menu items from a non-main thread.
Comment 2 Dmitry Kazakov 2024-06-05 14:18:32 UTC
Git commit 4e882db22bb0064b4faa353125928728bad66567 by Dmitry Kazakov.
Committed on 05/06/2024 at 14:18.
Pushed by dkazakov into branch 'master'.

Fix update of "read-only" state of the document when loading and saving

We shouldn't set the status of the actions explicitly from the
document, since it created thread-safety issues on MacOS
(and just doesn't work, since the enabled state is managed by
KisActionManager)

M  +3    -5    libs/ui/KisDocument.cpp
M  +13   -8    libs/ui/KisMainWindow.cpp
M  +1    -2    libs/ui/KisMainWindow.h
M  +1    -0    libs/ui/kis_action.h
M  +5    -0    libs/ui/kis_action_manager.cpp

https://invent.kde.org/graphics/krita/-/commit/4e882db22bb0064b4faa353125928728bad66567
Comment 3 Dmitry Kazakov 2024-06-05 14:20:26 UTC
Git commit a42d1eba5e660dc33c7abea40981195989c41ea1 by Dmitry Kazakov.
Committed on 05/06/2024 at 14:20.
Pushed by dkazakov into branch 'krita/5.2'.

Fix update of "read-only" state of the document when loading and saving

We shouldn't set the status of the actions explicitly from the
document, since it created thread-safety issues on MacOS
(and just doesn't work, since the enabled state is managed by
KisActionManager)

# Conflicts:
#	libs/ui/kis_action.h

M  +3    -5    libs/ui/KisDocument.cpp
M  +13   -8    libs/ui/KisMainWindow.cpp
M  +1    -2    libs/ui/KisMainWindow.h
M  +1    -0    libs/ui/kis_action.h
M  +5    -0    libs/ui/kis_action_manager.cpp

https://invent.kde.org/graphics/krita/-/commit/a42d1eba5e660dc33c7abea40981195989c41ea1