invokeLaterOnWriteThread Assertion failed: Write access is allowed inside write-action only

Answered

In my plugin, I am trying to modify a file to add additional text at the cursor.

I keep getting this stacktrace while testing; however, the text is inserted. This happens only for the first time the action is invoked. Subsequent times, I don't get this error. I tried using the following. The behavior was same in all cases.

ApplicationManager.getApplication().invokeLater(runnable)
ApplicationManager.getApplication().runWriteAction(runnable)
ApplicationManager.getApplication().invokeLaterOnWriteThread(runnable)

Throwable: Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())
    at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:220)
    at com.intellij.openapi.application.impl.ApplicationImpl.assertWriteAccessAllowed(ApplicationImpl.java:1336)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.watcher.RootsChangeWatcher$1.fireRootsChangeEvent(RootsChangeWatcher.kt:167)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.watcher.RootsChangeWatcher$1.fireRootsChangeEvent$default(RootsChangeWatcher.kt:166)
    at com.intellij.workspaceModel.ide.impl.legacyBridge.watcher.RootsChangeWatcher$1$prepareChange$2.afterVfsChange(RootsChangeWatcher.kt:120)
    at com.intellij.openapi.vfs.newvfs.AsyncEventSupport.afterVfsChange(AsyncEventSupport.java:133)
    at com.intellij.openapi.vfs.newvfs.AsyncEventSupport$1.after(AsyncEventSupport.java:65)
    at com.intellij.util.messages.impl.MessageBusImplKt.invokeMethod(MessageBusImpl.kt:649)
    at com.intellij.util.messages.impl.MessageBusImplKt.invokeListener(MessageBusImpl.kt:625)
    at com.intellij.util.messages.impl.MessageBusImplKt.executeOrAddToQueue(MessageBusImpl.kt:470)
    at com.intellij.util.messages.impl.ToDirectChildrenMessagePublisher.publish$intellij_platform_core(CompositeMessageBus.kt:259)
    at com.intellij.util.messages.impl.MessagePublisher.invoke(MessageBusImpl.kt:425)
    at jdk.proxy2/jdk.proxy2.$Proxy148.after(Unknown Source)
    at com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl.fireAfterEvents(PersistentFSImpl.java:1218)
    at com.intellij.openapi.vfs.newvfs.persistent.PersistentFSImpl$3.close(PersistentFSImpl.java:806)
    at com.intuit.genai.ide.intellij.handlers.webview.InsertAtCursorEventHandler.handleEvent$lambda$4$lambda$1(InsertAtCursorEventHandler.kt:75)
    at org.jetbrains.concurrency.AsyncPromise$onSuccess$1.accept(AsyncPromise.kt:84)
    at org.jetbrains.concurrency.AsyncPromise$onSuccess$1.accept(AsyncPromise.kt:16)
    at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
    at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887)
    at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2325)
    at org.jetbrains.concurrency.AsyncPromise.onSuccess(AsyncPromise.kt:78)
    at org.jetbrains.concurrency.AsyncPromise.onSuccess(AsyncPromise.kt:16)
    at com.intuit.genai.ide.intellij.handlers.webview.InsertAtCursorEventHandler.handleEvent$lambda$4(InsertAtCursorEventHandler.kt:34)
    at com.intellij.openapi.application.TransactionGuardImpl.runWithWritingAllowed(TransactionGuardImpl.java:209)
    at com.intellij.openapi.application.TransactionGuardImpl.access$100(TransactionGuardImpl.java:21)
    at com.intellij.openapi.application.TransactionGuardImpl$1.run(TransactionGuardImpl.java:191)
    at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:881)
    at com.intellij.openapi.application.impl.ApplicationImpl$3.run(ApplicationImpl.java:513)
    at com.intellij.openapi.application.impl.FlushQueue.doRun(FlushQueue.java:75)
    at com.intellij.openapi.application.impl.FlushQueue.runNextEvent(FlushQueue.java:118)
    at com.intellij.openapi.application.impl.FlushQueue.flushNow(FlushQueue.java:42)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:779)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:730)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:724)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:749)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:918)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:766)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$6(IdeEventQueue.java:450)
    at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:791)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$7(IdeEventQueue.java:449)
    at com.intellij.openapi.application.TransactionGuardImpl.performActivity(TransactionGuardImpl.java:105)
    at com.intellij.ide.IdeEventQueue.performActivity(IdeEventQueue.java:624)
    at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:447)
    at com.intellij.openapi.application.impl.ApplicationImpl.runIntendedWriteActionOnCurrentThread(ApplicationImpl.java:881)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:493)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:207)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:92)
2023-08-25 10:11:29,192 [ 228721] SEVERE - #c.i.o.a.i.ApplicationImpl - IntelliJ IDEA 2022.2.5  Build #IU-222.4554.10
2023-08-25 10:11:29,192 [ 228721] SEVERE - #c.i.o.a.i.ApplicationImpl - JDK: 17.0.6; VM: OpenJDK 64-Bit Server VM; Vendor: JetBrains s.r.o.
2023-08-25 10:11:29,192 [ 228721] SEVERE - #c.i.o.a.i.ApplicationImpl - OS: Mac OS X

 

This is my complete code

val text:String = eventPayload.getAsJsonPrimitive("text").asString;
var runnable = Runnable {
DataManager.getInstance().dataContextFromFocusAsync
.onSuccess(Consumer { context: DataContext ->
run {
val project: Project = context.getData(CommonDataKeys.PROJECT)!!
if (FileEditorManager.getInstance(project).selectedEditors.isEmpty()) {
Messages.showMessageDialog("No editor selected", "Error", Messages.getErrorIcon())
return@Consumer
}
var openFile: VirtualFile? = FileEditorManager.getInstance(project).selectedEditors[0].file
var fileEditor = FileEditorManager.getInstance(project).selectedEditors[0]
if (!(fileEditor is TextEditorImpl)) {
log.info("Context is not a text editor")
throw Exception("Context is not a text editor")
}
if (openFile != null) {
openFile.refresh(false, false)
var content = fileEditor.editor.document.text;
var location: TextEditorLocation = FileEditorManager.getInstance(project)
.selectedEditors[0].currentLocation as TextEditorLocation
var line: Int = location.position.line
var column: Int = location.position.column
log.debug("content is \n"+content)
var insertIndex = 0
var insertLine = 0
while (insertLine < line) {
insertIndex = content.indexOf("\n", insertIndex+1)
insertLine++;
log.debug("new insertIndex= "+insertIndex)
}
insertIndex += column;
log.debug("final insertIndex= "+insertIndex)
var insertText: String
if (insertIndex == 0 ){
insertText = text+content
} else {
var start: String = content.substring(0,insertIndex+1)
var end: String = content.substring(insertIndex+1)
insertText = start+text+end
}
var os: OutputStream = openFile!!.getOutputStream(null)
os.write(insertText.toByteArray())
os.flush()
os.close()
openFile.refresh(false, false)
}
}
})
.onError(Consumer { it: Throwable ->
run {
Messages.showMessageDialog("Error occured: " + it.message,
"Error", Messages.getErrorIcon())
}
})
}
ApplicationManager.getApplication().invokeLaterOnWriteThread(runnable)
0
1 comment

In what context is this whole code snippet called from? What is InsertAtCursorEventHandler?

Please work through https://plugins.jetbrains.com/docs/intellij/editor-basics.html guides that demonstrate manipulating editor/text. A lot of above code seems possibly superfluous (e.g., `openFile.refresh()` calls, handling with outputStream of file, etc.).

See also https://plugins.jetbrains.com/docs/intellij/general-threading-rules.html on Threading.

0

Please sign in to leave a comment.