Quit Application Menu item and modal dialogs on macOS

I'm trying to understand the mechanism by which the Quit Application menu item gets disabled while a modal dialog/alert is up.

Who is responsible for disabling the menu item?

We have a case where in some cases, the Quit Application menu item is not getting disabled even when a modal dialog is up. So I'm trying to figure out where things may be going wrong.