This issue was caused by the updating of the caret position after the paste occurs firing an
updateposition event resulting in an
GeckoView:ShowSelectionAction message to reposition context menu without any attempt to close the context menu. The solution is to hide the context menu before the repositioning of the caret occurs such that the
updateposition event doesn't trigger a redisplay.
I tried several versions of this trying to trigger the menu close from inside
GeckoViewSelectionActionDelegate, including making the call to
docShell.doCommand("cmd_paste"); asynchronous and firing
ACTION_HIDE before firing
ACTION_PASTE, but the result is always the same - one of the events is processed before the other and so the second event is rejected as a stale response.
Therefore we are firing the
pagehide from inside the code that performs the paste. This has to be done before the paste occurs otherwise the
updateposition event is fired before the page hide event and the context menu redisplays before being hidden.