Re: RFR: JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen [v2]

2024-02-28 Thread Marius Hanl
On Wed, 28 Feb 2024 20:20:35 GMT, Martin Fox  wrote:

> I don't think this is a problem with the nested event loop bookkeeping. It 
> looks like a much simpler bug in the invokeLaterDispatcher.

Well, yes and no. My testing was showing that `notifyLeftNestedEventLoop` was 
not even called in the finally block, so there is that. We may still have a 
problem at the `InvokeLaterDispatcher`, although not 100% sure since it worked 
good for me on Windows.
But I will test it more as soon as I have more time, since I want to really 
check everything, so I do not miss anything here.

-

PR Comment: https://git.openjdk.org/jfx/pull/1324#issuecomment-1969990780


Re: RFR: JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen [v2]

2024-02-28 Thread Martin Fox
On Thu, 22 Feb 2024 23:13:26 GMT, Marius Hanl  wrote:

>> This PR fixes the dialog freeze problem once and for all. 
>> 
>> This one is a bit tricky to understand, here is how it works:
>> This bug happens on every platform, although the implementation of nested 
>> event loops differs on every platform.
>> E.g. on Linux we use `gtk_main` and `gtk_main_quit`, on Windows and Mac we 
>> have an own implementation of a nested event loop (while loop), controlled 
>> by a boolean flag.
>> 
>> Funny enough, the reason why this bug happens is always the same: Timing.
>> 
>> 1. When we hide a dialog, `_leaveNestedEventLoop` is called. 
>> 2. This will call native code to get out of the nested event loop, e.g. on 
>> Windows we try to break out of the while loop with a boolean flag, on Linux 
>> we call `gtk_main_quit`.
>> 3. Now, if we immediately open a new dialog, we enter a new nested event 
>> loop via `_enterNestedEventLoop`, as a consequence we do not break out of 
>> the while loop on Windows (the flag is set back again, the while loop is 
>> still running), and we do not return from `gtk_main` on Linux.
>> 4. And this will result in the Java code never returning and calling 
>> `notifyLeftNestedEventLoop`, which we need to recover the UI.
>> 
>> So it is actually not trivial to fix this problem, and we can not really do 
>> anything on the Java side. We may can try to wait until one more frame has 
>> run so that things will hopefully be right, but that sounds rather hacky.
>> 
>> I therefore analyzed, if we even need to return from 
>> `_enterNestedEventLoop`. Turns out, we don't need to. 
>> There is a return value which we actually do not use (it does not have any 
>> meaning to us, other that it is used inside an assert statement).
>> Without the need of a return value, we also do not need to care when 
>> `_enterNestedEventLoop` is returning - instead we cleanup and call 
>> `notifyLeftNestedEventLoop` in `_leaveNestedEventLoop`, after the native 
>> code was called.
>> 
>> Lets see if this is the right approach (for all platforms).
>> Testing appreciated.
>> #
>> - [x] Tested on Windows
>> - [x] Tested on Linux
>> - [x] Tested on Mac
>> - [ ] Tested on iOS (although the nested event loop code is the same as for 
>> Mac) (I would appreciate if someone can do this as I have no access to an 
>> iOS device)
>> - [ ] Adjust copyright
>> - [ ] Write Systemtest
>
> Marius Hanl has updated the pull request with a new target base due to a 
> merge or a rebase. The incremental webrev excludes the unrelated changes 
> brought in by the merge/rebase. The pull request contains three additional 
> commits since the last revision:
> 
>  - Merge remote-tracking branch 'openjfx/master' into 
> JDK-8285893-dialog-freezing-略
>  - JDK-8285893: Decrement nestedEventLoopCounter in leaveNestedEventLoop
>  - JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen

I don't think this is a problem with the nested event loop bookkeeping. It 
looks like a much simpler bug in the invokeLaterDispatcher.

When exitNestedEventLoop is called on the innermost loop the 
invokeLaterDispatcher suspends operation until the loop finishes. But if you 
immediately start a new event loop the previous one won't finish and the 
dispatcher will be wedged. If you're already in a nested loop you can get into 
the wedged state with just two lines of code:

`exitNestedEventLoop(innermost, retVal); enterNestedEventLoop(newLoop);`

When the invokeLaterDispatcher is told that the innermost loop is exiting it 
currently sets `leavingNestedEventLoop` to true. When the dispatcher is told 
that a new event loop has started it is *not* clearing `leavingNestedEventLoop` 
but it should. Basically it should follow the same logic used in glass; leaving 
the innermost loop updates a boolean indicating that the loop should exit but 
if a new loop is started the boolean is set back to a running state since it 
now applies to the new loop, not the previous one.

-

PR Comment: https://git.openjdk.org/jfx/pull/1324#issuecomment-1969844034


Re: RFR: JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen [v2]

2024-02-27 Thread Martin Fox
On Thu, 22 Feb 2024 23:13:26 GMT, Marius Hanl  wrote:

>> This PR fixes the dialog freeze problem once and for all. 
>> 
>> This one is a bit tricky to understand, here is how it works:
>> This bug happens on every platform, although the implementation of nested 
>> event loops differs on every platform.
>> E.g. on Linux we use `gtk_main` and `gtk_main_quit`, on Windows and Mac we 
>> have an own implementation of a nested event loop (while loop), controlled 
>> by a boolean flag.
>> 
>> Funny enough, the reason why this bug happens is always the same: Timing.
>> 
>> 1. When we hide a dialog, `_leaveNestedEventLoop` is called. 
>> 2. This will call native code to get out of the nested event loop, e.g. on 
>> Windows we try to break out of the while loop with a boolean flag, on Linux 
>> we call `gtk_main_quit`.
>> 3. Now, if we immediately open a new dialog, we enter a new nested event 
>> loop via `_enterNestedEventLoop`, as a consequence we do not break out of 
>> the while loop on Windows (the flag is set back again, the while loop is 
>> still running), and we do not return from `gtk_main` on Linux.
>> 4. And this will result in the Java code never returning and calling 
>> `notifyLeftNestedEventLoop`, which we need to recover the UI.
>> 
>> So it is actually not trivial to fix this problem, and we can not really do 
>> anything on the Java side. We may can try to wait until one more frame has 
>> run so that things will hopefully be right, but that sounds rather hacky.
>> 
>> I therefore analyzed, if we even need to return from 
>> `_enterNestedEventLoop`. Turns out, we don't need to. 
>> There is a return value which we actually do not use (it does not have any 
>> meaning to us, other that it is used inside an assert statement).
>> Without the need of a return value, we also do not need to care when 
>> `_enterNestedEventLoop` is returning - instead we cleanup and call 
>> `notifyLeftNestedEventLoop` in `_leaveNestedEventLoop`, after the native 
>> code was called.
>> 
>> Lets see if this is the right approach (for all platforms).
>> Testing appreciated.
>> #
>> - [x] Tested on Windows
>> - [x] Tested on Linux
>> - [x] Tested on Mac
>> - [ ] Tested on iOS (although the nested event loop code is the same as for 
>> Mac) (I would appreciate if someone can do this as I have no access to an 
>> iOS device)
>> - [ ] Adjust copyright
>> - [ ] Write Systemtest
>
> Marius Hanl has updated the pull request with a new target base due to a 
> merge or a rebase. The incremental webrev excludes the unrelated changes 
> brought in by the merge/rebase. The pull request contains three additional 
> commits since the last revision:
> 
>  - Merge remote-tracking branch 'openjfx/master' into 
> JDK-8285893-dialog-freezing-略
>  - JDK-8285893: Decrement nestedEventLoopCounter in leaveNestedEventLoop
>  - JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen

The Mac is failing on the simplest NestedEventLoop test, the one that verifies 
that we can enter and exit a nested loop and correctly retrieve the return 
value. I started looking into this but find what's happening in Glass a bit 
confusing. I'll take a closer look in the next day or two.

I don't understand the original failure case. Events aren't blocked and you can 
type into the modal's text field, it just doesn't repaint. If you resize the 
window you will see your edits.

-

PR Comment: https://git.openjdk.org/jfx/pull/1324#issuecomment-1967605288


Re: RFR: JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen [v2]

2024-02-24 Thread Marius Hanl
On Fri, 23 Feb 2024 17:39:44 GMT, Kevin Rushforth  wrote:

> This changes one of the fundamental aspects of entering and exiting a nested 
> event loop. This will need a lot more analysis to show why this is the right 
> approach.

Yes, this is right and I also thought about documenting that as soon as we 
think that this is the right approach.
I completely agree that we need to analyse this. This is not a simple change 
but will affect Dialogs and every custom code that uses nested event loops 
(e.g. I used it for blocking the UI without freezing it for data loading 
purposes, where we want to wait until the code returns in a non UI blicking 
way).

Maybe there is a better way as well, I just don't figured out one. 
As described, right now we rely that `enterNestedEventLoop` returns always, 
which is not happening immediately for ALL platforms, even though Platform has 
another implementation. Which makes me think the current approach has a flaw, 
resulting in this behaviour.

> I see the same failure with the most recent patch as I did yesterday. I 
> looked over the changes, and I'm not convinced that this is the right 
> approach.

Thanks for testing. As soon as my colleague is available, I will debug this 
issue. I don't get why only MacOS is failing, since the low level code is 
pretty much the same, we just continue right after we left the nested event 
loop.
I agree that we should find out the root cause why MacOS behaves different.

> I don't know of any supported way to run macOS in a VM on a Windows host, but 
> perhaps others will be able to help.

That was also my understanding the last time I checked. AFAIK, think there is 
no official way from Apple, but VirtualBox may offer that. Will check as soon 
as I have more time.

-

PR Comment: https://git.openjdk.org/jfx/pull/1324#issuecomment-1962428672


Re: RFR: JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen [v2]

2024-02-23 Thread Kevin Rushforth
On Thu, 22 Feb 2024 23:13:26 GMT, Marius Hanl  wrote:

>> This PR fixes the dialog freeze problem once and for all. 
>> 
>> This one is a bit tricky to understand, here is how it works:
>> This bug happens on every platform, although the implementation of nested 
>> event loops differs on every platform.
>> E.g. on Linux we use `gtk_main` and `gtk_main_quit`, on Windows and Mac we 
>> have an own implementation of a nested event loop (while loop), controlled 
>> by a boolean flag.
>> 
>> Funny enough, the reason why this bug happens is always the same: Timing.
>> 
>> 1. When we hide a dialog, `_leaveNestedEventLoop` is called. 
>> 2. This will call native code to get out of the nested event loop, e.g. on 
>> Windows we try to break out of the while loop with a boolean flag, on Linux 
>> we call `gtk_main_quit`.
>> 3. Now, if we immediately open a new dialog, we enter a new nested event 
>> loop via `_enterNestedEventLoop`, as a consequence we do not break out of 
>> the while loop on Windows (the flag is set back again, the while loop is 
>> still running), and we do not return from `gtk_main` on Linux.
>> 4. And this will result in the Java code never returning and calling 
>> `notifyLeftNestedEventLoop`, which we need to recover the UI.
>> 
>> So it is actually not trivial to fix this problem, and we can not really do 
>> anything on the Java side. We may can try to wait until one more frame has 
>> run so that things will hopefully be right, but that sounds rather hacky.
>> 
>> I therefore analyzed, if we even need to return from 
>> `_enterNestedEventLoop`. Turns out, we don't need to. 
>> There is a return value which we actually do not use (it does not have any 
>> meaning to us, other that it is used inside an assert statement).
>> Without the need of a return value, we also do not need to care when 
>> `_enterNestedEventLoop` is returning - instead we cleanup and call 
>> `notifyLeftNestedEventLoop` in `_leaveNestedEventLoop`, after the native 
>> code was called.
>> 
>> Lets see if this is the right approach (for all platforms).
>> Testing appreciated.
>> #
>> - [x] Tested on Windows
>> - [x] Tested on Linux
>> - [x] Tested on Mac
>> - [ ] Tested on iOS (although the nested event loop code is the same as for 
>> Mac) (I would appreciate if someone can do this as I have no access to an 
>> iOS device)
>> - [ ] Adjust copyright
>> - [ ] Write Systemtest
>
> Marius Hanl has updated the pull request with a new target base due to a 
> merge or a rebase. The incremental webrev excludes the unrelated changes 
> brought in by the merge/rebase. The pull request contains three additional 
> commits since the last revision:
> 
>  - Merge remote-tracking branch 'openjfx/master' into 
> JDK-8285893-dialog-freezing-略
>  - JDK-8285893: Decrement nestedEventLoopCounter in leaveNestedEventLoop
>  - JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen

I see the same failure with the most recent patch as I did yesterday. I looked 
over the changes, and I'm not convinced that this is the right approach.

One comment on your proposal:

> we also do not need to care when _enterNestedEventLoop is returning - instead 
> we cleanup and call notifyLeftNestedEventLoop in _leaveNestedEventLoop, after 
> the native code was called.

This changes one of the fundamental aspects of entering and exiting a nested 
event loop. This will need a lot more analysis to show why this is the right 
approach.

-

PR Comment: https://git.openjdk.org/jfx/pull/1324#issuecomment-1961735156


Re: RFR: JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen [v2]

2024-02-22 Thread Marius Hanl
> This PR fixes the dialog freeze problem once and for all. 
> 
> This one is a bit tricky to understand, here is how it works:
> This bug happens on every platform, although the implementation of nested 
> event loops differs on every platform.
> E.g. on Linux we use `gtk_main` and `gtk_main_quit`, on Windows and Mac we 
> have an own implementation of a nested event loop (while loop), controlled by 
> a boolean flag.
> 
> Funny enough, the reason why this bug happens is always the same: Timing.
> 
> 1. When we hide a dialog, `_leaveNestedEventLoop` is called. 
> 2. This will call native code to get out of the nested event loop, e.g. on 
> Windows we try to break out of the while loop with a boolean flag, on Linux 
> we call `gtk_main_quit`.
> 3. Now, if we immediately open a new dialog, we enter a new nested event loop 
> via `_enterNestedEventLoop`, as a consequence we do not break out of the 
> while loop on Windows (the flag is set back again, the while loop is still 
> running), and we do not return from `gtk_main` on Linux.
> 4. And this will result in the Java code never returning and calling 
> `notifyLeftNestedEventLoop`, which we need to recover the UI.
> 
> So it is actually not trivial to fix this problem, and we can not really do 
> anything on the Java side. We may can try to wait until one more frame has 
> run so that things will hopefully be right, but that sounds rather hacky.
> 
> I therefore analyzed, if we even need to return from `_enterNestedEventLoop`. 
> Turns out, we don't need to. 
> There is a return value which we actually do not use (it does not have any 
> meaning to us, other that it is used inside an assert statement).
> Without the need of a return value, we also do not need to care when 
> `_enterNestedEventLoop` is returning - instead we cleanup and call 
> `notifyLeftNestedEventLoop` in `_leaveNestedEventLoop`, after the native code 
> was called.
> 
> Lets see if this is the right approach (for all platforms).
> Testing appreciated.
> #
> - [x] Tested on Windows
> - [x] Tested on Linux
> - [x] Tested on Mac
> - [ ] Tested on iOS (although the nested event loop code is the same as for 
> Mac) (I would appreciate if someone can do this as I have no access to an iOS 
> device)
> - [ ] Adjust copyright
> - [ ] Write Systemtest

Marius Hanl has updated the pull request with a new target base due to a merge 
or a rebase. The incremental webrev excludes the unrelated changes brought in 
by the merge/rebase. The pull request contains three additional commits since 
the last revision:

 - Merge remote-tracking branch 'openjfx/master' into 
JDK-8285893-dialog-freezing-略
 - JDK-8285893: Decrement nestedEventLoopCounter in leaveNestedEventLoop
 - JDK-8285893: Hiding dialog and showing new one causes dialog to be frozen

-

Changes:
  - all: https://git.openjdk.org/jfx/pull/1324/files
  - new: https://git.openjdk.org/jfx/pull/1324/files/98a97b0a..cb8865e8

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jfx=1324=01
 - incr: https://webrevs.openjdk.org/?repo=jfx=1324=00-01

  Stats: 294903 lines in 6630 files changed: 173936 ins; 83950 del; 37017 mod
  Patch: https://git.openjdk.org/jfx/pull/1324.diff
  Fetch: git fetch https://git.openjdk.org/jfx.git pull/1324/head:pull/1324

PR: https://git.openjdk.org/jfx/pull/1324