On Fri, 19 Jan 2024 01:43:48 GMT, Sergey Bylokhov <[email protected]> wrote:
>> Somehow we have null passed in.
>> So something looked up appcontext and got null.
>> Likely because some other code removed the app context from a map or
>> similar.
>> First step is to find exactly where that happened, then we can start to
>> reason about how we managed to still post an event.
>
> Note that it does not fail with NPE but instead this code trigger the bug:
>
> AppContext eventContext = targetToAppContext(event.getSource());
> if (eventContext != null && !eventContext.equals(appContext)) {
> throw new RuntimeException("Event posted on wrong app context : "
> + event);
> }
>
> The source of the event is TrayIcon, and the appconteext for it is non-null
> as well.
>
> This is the code where we post an event:
>
> @Override
> public void dispose() {
> dummyFrame.dispose();
>
> if (popup != null) {
> popup.removeNotify();
> }
>
> LWCToolkit.targetDisposedPeer(target, this);
> target = null;
>
> super.dispose();
> }
>
> private void postEvent(final AWTEvent event) {
> SunToolkit.executeOnEventHandlerThread(target, new Runnable() {
> public void run() {
> SunToolkit.postEvent(SunToolkit.targetToAppContext(target),
> event);
> }
> });
> }
>
>
> Note that we also call SunToolkit.targetToAppContext(target), and the target
> should be a TrayIcon and have a non-null appcontext, because later we
> successfully fetch it.
>
>
> So the problem here is not in the null appcontext, but that we set target to
> null in the peer in dispose(), at the time we still processing the events.
> - So we should check how all this should be synchronized, to prevent such
> race
> - Or we should not set target to null as on Windows
> - Or call dispose on EDT like on Linux.
I think we should set the target once and use it forever.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/17329#discussion_r1458186579