Psi/Document inconsistency

Hi,

We're developing plugin for our language, and I've got some problem, I cannot explain. I have the code that generates some other code and inserts it into document :

ApplicationManager.getApplication().invokeLater(new Runnable() { public void run() {     ApplicationManager.getApplication().runWriteAction(new Runnable() {         public void run() {             if(metaUsage.isValid()) { // can become not valid                 generating = true;                 LSFMetaCodeBody body = metaUsage.getMetaCodeBody();                 if(fMetaText==null) {                     if(body!=null)                         body.delete();                 } else {                     LSFMetaCodeBody genBody = LSFElementGenerator.createMetaBodyFromText(myProject, fMetaText);                     if(body != null) {                         body.replace(genBody);                     } else {                         metaUsage.getNode().addChild(genBody.getNode(), metaUsage.getMetaCodeIdList().getNode().getTreeNext().getTreeNext());                     }                 }                 generating = false;             }         }     }); } });


And when running body.replace(genBody), or adding child, at first I get the following error:

[ 252914]  ERROR - impl.PsiToDocumentSynchronizer - Attempt to modify PSI for non-committed Document! java.lang.Throwable      at com.intellij.openapi.diagnostic.Logger.error(Logger.java:54)      at com.intellij.psi.impl.PsiToDocumentSynchronizer.a(PsiToDocumentSynchronizer.java:85)      at com.intellij.psi.impl.PsiToDocumentSynchronizer.childReplaced(PsiToDocumentSynchronizer.java:133)      at com.intellij.psi.impl.PsiManagerImpl.a(PsiManagerImpl.java:453)      at com.intellij.psi.impl.PsiManagerImpl.childReplaced(PsiManagerImpl.java:354)      at com.intellij.pom.wrappers.PsiEventWrapperAspect.a(PsiEventWrapperAspect.java:102)      at com.intellij.pom.wrappers.PsiEventWrapperAspect.update(PsiEventWrapperAspect.java:49)      at com.intellij.pom.core.impl.PomModelImpl.runTransaction(PomModelImpl.java:156)      at com.intellij.psi.impl.source.tree.ChangeUtil.prepareAndRunChangeAction(ChangeUtil.java:177)      at com.intellij.psi.impl.source.tree.CompositeElement.replaceChild(CompositeElement.java:717)      at com.intellij.psi.impl.source.codeStyle.CodeEditUtil.replaceChild(CodeEditUtil.java:195)      at com.intellij.extapi.psi.ASTDelegatePsiElement.replaceChildInternal(ASTDelegatePsiElement.java:386)      at com.intellij.extapi.psi.ASTDelegatePsiElement.replace(ASTDelegatePsiElement.java:376)      at com.simpleplugin.psi.MetaChangeDetector$MetaUsageProcessing$1$1$1.run(MetaChangeDetector.java:204)      at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:1013)      at com.simpleplugin.psi.MetaChangeDetector$MetaUsageProcessing$1$1.run(MetaChangeDetector.java:192)      at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:343)      at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:646)      at java.awt.EventQueue.access$000(EventQueue.java:84)      at java.awt.EventQueue$1.run(EventQueue.java:607)      at java.awt.EventQueue$1.run(EventQueue.java:605)      at java.security.AccessController.doPrivileged(Native Method)      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)      at java.awt.EventQueue.dispatchEvent(EventQueue.java:616)      at com.intellij.ide.IdeEventQueue.d(IdeEventQueue.java:686)      at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:511)      at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:334)      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


And then after that I am constantly getting:

[ 319811]  ERROR - .psi.impl.DocumentCommitThread - PSI/document inconsistency before reparse: file=Lsf File java.lang.AssertionError: PSI/document inconsistency before reparse: file=Lsf File      at com.intellij.psi.impl.DocumentCommitThread.a(DocumentCommitThread.java:648)      at com.intellij.psi.impl.DocumentCommitThread.a(DocumentCommitThread.java:576)      at com.intellij.psi.impl.DocumentCommitThread.access$600(DocumentCommitThread.java:67)      at com.intellij.psi.impl.DocumentCommitThread$3.run(DocumentCommitThread.java:460)      at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1182)      at com.intellij.psi.impl.DocumentCommitThread.a(DocumentCommitThread.java:473)      at com.intellij.psi.impl.DocumentCommitThread.access$500(DocumentCommitThread.java:67)      at com.intellij.psi.impl.DocumentCommitThread$2.run(DocumentCommitThread.java:352)      at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:226)      at com.intellij.psi.impl.DocumentCommitThread.b(DocumentCommitThread.java:349)      at com.intellij.psi.impl.DocumentCommitThread.run(DocumentCommitThread.java:293)      at java.lang.Thread.run(Thread.java:662)


What I don't get is how document can become inconsistent, when the whole process of changing PSI is wrapped into runWriteAction, and it acquires global lock, and after its completion, commit document synchronously. Or am I wrong, and the last process is asynchronous?

PS: This exception i get, only when I run described code very very often (for example press the button and don't realease it, what triggers described event "continously").

6 comments

Wrapping everything into performForCommittedDocument, or commit document before helps (and commiting after doesn't), but giving to that such behaviour is really not safe, because the other processes may work with incorrect psi without knowing it (isValid will be true). Or I am missing something?

0

Committing the document means building an up-to-date PSI structure from the document contents. If you don't commit the document before you start modifying the PSI, you're working with an out-of-date PSI structure that doesn't correspond to the actual contents of the document, which is what the exception is telling you. Committing the document after you modify the PSI makes no sense, because you've already changed the PSI structure.

PSI modifications can be performed only in the event dispatch thread while holding a write action. A write action is exclusive, which means that there can be no other processes running under a read action in parallel to your code. And if someone holds a PSI element without holding a read action, then this element can become invalid at any point in time, which is expected behavior.

0

So the only safe case for modification action is the following:

UIUtil.invokeLaterIfNeeded - because we can run PSI modification only in main UI thread
DocumentManage.performForCommitedDocument - because the document may be not-commited, and we'll get the described exception
Application.runWriteAction - we need to block read actions (to prevent them from getting dirty data). Write actions may only be executed in current thread, so their blocking is a sort of "side effect".

Eliminating any of these steps in common case will lead to exceptions. Am I right?

0

Committing the document after you modify the PSI makes no sense, because you've already changed the PSI structure.

And what about text offsets and so on...

It was discussed here:
http://confluence.jetbrains.com/display/IDEADEV/IntelliJ+IDEA+Architectural+Overview?focusedCommentId=20816#comment-20816

0

I'm not sure that using performForCommittedDocument() would be correct in your case. It adds your runnable to the list of runnables which will be eventually executed when the document is committed. Most likely you just need to call commitDocument() and then perform your operation.

0

What about text offsets? This only matters if you need to perform some modifications through the document immediately after some PSI modifications. Since you're only working with the PSI, this shouldn't matter to you at all.

0

Please sign in to leave a comment.