Modal execution isn't so modal

I’ve just realised that I can have a long-ish process going on under executeAsModal, working on a document, and that I can easily close that document while it’s processing, just by pressing ctrl-W.

Then I get any one of several error messages, the most obvious being something like “The document with an id of 4652 does not exist”.

For the record, I am awaiting on the executeAsModal call, and I’m not setting the interactive option. I’m also not calling registerAutoCloseDocument.

According to the docs, using executeAsModal “means that executeAsModal guarantees that the plugin gets exclusive access to Photoshop.”

Before I dig in and start looking for ways to respond safely to this in my code, is there anything that I can do to lock that document down better? I do already have suspendHistory set on it.

I guess I never read the ‘modal’ in executeAsModal to be quite the same as a modal UI. I think it merely means that Photoshop blocks other plugins from doing any edits, and as such preventing race conditions in the Photoshop state. To be honest I would expect Photoshop to allow interrupts, such as Ctrl+W or Ctrl+Q, otherwise a long-running task could never be interrupted other than by force-quitting the application. That said I would ideally expect Photoshop to bring up an alert of some sort (.e.g. alerting to the fact that a plugin interacts with a document that’s about to be closed), giving the user the option to continue on waiting or cancelling out :thinking:

Well, the user can cancel using escape or the cancel button on the progress bar, and the docs do promise exclusive access in modal mode.

FWIW I just tried adding a notification listener on closing docs. I sort of works, but it’s too slow; much of the time I was hitting the error before the listener had time to jump in. So I’ve built in code for missing documents and layers into my error handler.

Unfortunately that’s not enough; it seems that pretty much anything that can be requested with key strokes in Photoshop can still be requested and acted on while a plugin is executing modally. As an example a plugin does a select all to fill a layer; pressing ctrl-D at just the right (wrong?) time can clear that selection so that the fill fails.

I think I may have hit the limit on what it’s worth doing here; the only thing I can see to defend against that last (and, admittedly probably very rare) situation is to loop and re-select if the fill fails, but that has the potential for some nasty side-effects in itself.