# Modal execution isn't so modal

**URL:** <https://forums.creativeclouddeveloper.com/t/modal-execution-isnt-so-modal/7603>\
**Category:** Photoshop\
**Created:** [February 28, 2024, 1:31pm UTC](https://forums.creativeclouddeveloper.com/t/modal-execution-isnt-so-modal/7603 "2024-02-28T13:31:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MartinB](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/martinb/32/3506_2.png) [@MartinB](https://forums.creativeclouddeveloper.com/u/MartinB)\
**Post date:** [February 28, 2024, 1:31pm UTC](https://forums.creativeclouddeveloper.com/t/modal-execution-isnt-so-modal/7603/1 "2024-02-28T13:31:26Z")

</div>

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](https://developer.adobe.com/photoshop/uxp/2022/ps_reference/media/executeasmodal/), 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.

---

<div class="post-metadata">

**Author:** ![dotproduct](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@dotproduct](https://forums.creativeclouddeveloper.com/u/dotproduct)\
**Post date:** [February 28, 2024, 6:45pm UTC](https://forums.creativeclouddeveloper.com/t/modal-execution-isnt-so-modal/7603/2 "2024-02-28T18:45:52Z")

</div>

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 🤔

---

<div class="post-metadata">

**Author:** ![MartinB](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/martinb/32/3506_2.png) [@MartinB](https://forums.creativeclouddeveloper.com/u/MartinB)\
**Post date:** [March 2, 2024, 3:43pm UTC](https://forums.creativeclouddeveloper.com/t/modal-execution-isnt-so-modal/7603/3 "2024-03-02T15:43:04Z")

</div>

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.
