Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix possible hierarchy corruption during removing ComposePanel with LayerType.OnComponent #1132

Draft
wants to merge 1 commit into
base: jb-main
Choose a base branch
from

Conversation

MatkovIvan
Copy link
Member

Proposed Changes

  • dispose might trigger hierarchy change for example removing LayerType.OnComponent layers. That will conflict with removing this ComposePanel. To avoid such issues we need to call dispose after leaving this method.

Testing

Test: remove ComposePanel when it has opened layers attached to its parent

@MatkovIvan MatkovIvan force-pushed the ivan.matkov/fix-corrupt-on-remove branch from 17f5197 to fff3e56 Compare February 21, 2024 15:01
// [dispose] might trigger hierarchy change for example removing [LayerType.OnComponent]
// layers. That will conflict with removing this [ComposePanel]. To avoid such
// issues we need to call [dispose] after leaving this method.
SwingUtilities.invokeLater {
Copy link
Collaborator

@igordmn igordmn Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using any dispatching should be the last resort, as it introduces race conditions. One of the race conditions that came to my mind is:

val frame = JFrame()
frame.isVisible = true

val panel = ComposePanel()
frame.add(panel) // addNotify, init scene
frame.remove(panel) // removeNotify, dispatch dispose
frame.add(panel) // reinit scene, leak the old scene
...
// dispose the new scene

We can write a warding code for this race, but let's discuss alternatives first. Do I understand correctly that we have an issue with this hierarchy:

Window
  ContentPane
    ComposePanel
    Layer1  // created/removed by ComposePanel
    Layer2  // created/removed by ComposePanel

Do we have a crash or it is incorrect behaviour because of internal AWT state corruption?

Will moving layers into its own container help?

Window
  ContentPane
    ComposePanel
    Layers
      Layer1  // created/removed by ComposePanel
      Layer2  // created/removed by ComposePanel

Also, worth to write a test, if it's not difficult.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another alternative - disposing the scene without dispatching, but dispatch removing of the layers if they are removed in removeNotify (with the same race warding). It will be more safe, and closer to the issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a crash or it is incorrect behaviour because of internal AWT state corruption?

Some kind of. Sometimes it's an out-of-bounds crash, sometimes it just leads to a corrupted state without a crash

Will moving layers into its own container help?

  1. It's controlled by the user
  2. Layers (windowContainer) should be logically one of the parents of ComposePanel

but yes, it will avoid calling remove inside another remove call of the same container

Also, worth to write a test, if it's not difficult.

Sure. Initially this PR is a push of not planned fix from local stash with the main intention to discuss the problem. Let me convert it to draft to make it more clear

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disposing the scene without dispatching, but dispatch removing of the layers

It was in my head as a variant too

@MatkovIvan MatkovIvan marked this pull request as draft February 21, 2024 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants