Plugin State Not Persisted on IDE Close
I maintain a plugin that tracks IntelliJ activity and periodically uploads data to a server. (Source code here: https://github.com/cs125-illinois/intellijlogger.)
We upload logs periodically and also whenever a monitored project is opened or closed. This is done by creating a Task.Backgroundable and running it using the current ProgressManager. To ensure that we don't lose logs across restarts, logs are persisted using a PersistentStateComponent. And to ensure that we don't upload the same logs multiple times, logs are marked as completed once an upload completes successfully.
This all works fairly well. However, recently I noticed that we were receiving a lot of duplicate logs. I've determined that this is caused by logs that are uploaded when the project closes not being marked as completed, meaning that they are uploaded (again) the next time the IDE is restarted. In my local testing I'm also able to determine that this only seems to happen when the entire IDE is shut down, not when the project is closed (returning IntelliJ to the project selection menu).
More specifically, when I close the project and return to the menu I see something like this in my logs:
- Upload beginning
- Upload completed (logs are marked as done at this point)
- State saved
However, when I close the entire IDE I see something more like this:
- State saved
- Upload beginning
- Upload completed
- (No additional call to state saved is recorded, meaning that logs are not marked as done.)
I'm assuming that this is due to some race condition on shutdown between the logs being uploaded and the state being saved. However, I find it weird that the upload does seem to complete reliably—at least in my local testing where it's not pushing that much data—while the state never seems to get saved. I also tried moving our shutdown logic into projectClosedBeforeSave, and placing a manual call to project.save() after the logs are uploaded, but neither one of those changes seemed to help.
Blocking the project from closing seems like it's an option, but that's not really what I want either.
I can live with this for now since the duplicates can be removed during postprocessing. But I'd like to determine the right way to approach this situation.
请先登录再写评论。