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

Make usage of stateful patterns thread-safe #2633

Merged
merged 4 commits into from Mar 27, 2024

Conversation

SirYwell
Copy link
Member

@SirYwell SirYwell commented Mar 17, 2024

Overview

Description

Some patterns make use of MutableBlockVector or might delegate to patterns that might do so, therefore we need to fork it to prevent race conditions.

The example command //set #!z[#linear2d[white_wool,2%light_gray_wool,4%gray_wool,8%black_wool]] and how it looks:

Before:
baseline

With this change:
patched

Submitter Checklist

Edit tasklist title
Beta Give feedback Tasklist Submitter Checklist, more options

Delete tasklist

Delete tasklist block?
Are you sure? All relationships in this tasklist will be removed.
  1. Make sure you are opening from a topic branch (/feature/fix/docs/ branch (right side)) and not your main branch.
    Options
  2. Ensure that the pull request title represents the desired changelog entry.
    Options
  3. New public fields and methods are annotated with @since TODO.
    Options
  4. I read and followed the contribution guidelines.
    Options

@SirYwell SirYwell requested a review from a team as a code owner March 17, 2024 15:49
@github-actions github-actions bot added the Bugfix This PR fixes a bug label Mar 17, 2024
@dordsor21
Copy link
Member

This wouldn't work if the patterns are nested within another pattern. We ought to create an AbstractDelegatePattern and override fork in there and ensure all patterns with nested patterns utilise this

@SirYwell
Copy link
Member Author

Good point, I'll look into it.

@SirYwell SirYwell marked this pull request as draft March 17, 2024 16:54
@SirYwell SirYwell changed the title Make usage of No(Axis)Patterns thread-safe Make usage of stateful patterns thread-safe Mar 17, 2024
@SirYwell
Copy link
Member Author

I went for an interface StatefulPattern, there are more scenarios where patterns need forking and the ones delegating to other patterns are mostly not a good fit for an AbstractDelegatePattern (e.g. RandomPattern) and might also already extend other classes (e.g. AbstractExtentPattern, do we need to fork them?).

Let me know what you think.

@SirYwell
Copy link
Member Author

This might also be relevant for #2626, I'm just not sure what to expect from that pattern in a multi-threaded setting

@dordsor21
Copy link
Member

Are there definitely no further instances where pattern might not necessarily get forked here?

@SirYwell
Copy link
Member Author

Are there definitely no further instances where pattern might not necessarily get forked here?

I'll have to go through the WE patterns again, it seems there are a few more

@SirYwell
Copy link
Member Author

Went through the impls again, should be all now hopefully.

@SirYwell SirYwell marked this pull request as ready for review March 18, 2024 16:36
@dordsor21
Copy link
Member

Is there much of a point in having the new interface? If a new pattern that would use it is created then either we remember the interface or we don't (same chances as overriding the fork method tbh)

@SirYwell
Copy link
Member Author

I thought about forking some of the patterns only if the inner patterns need to be forked, in which case it was somewhat useful as a marker interface, but I decided against the additional checking overhead, so I guess it can be removed again.

@Override
public Pattern fork() {
RelativePattern forked = new RelativePattern(this.pattern.fork(), this.minY, this.maxY);
forked.origin = this.origin; // maintain origin for forks
Copy link
Member

@dordsor21 dordsor21 Mar 20, 2024

Choose a reason for hiding this comment

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

Should probably look at having the origin be toImmutable()ised when it's set (for this and any other patterns)

Copy link
Member Author

Choose a reason for hiding this comment

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

That would otherwise already cause bugs I think? But yeah, that might make sense.

Copy link
Member

Choose a reason for hiding this comment

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

Possibly, but can't be too careful aha

@dordsor21 dordsor21 requested a review from a team March 27, 2024 09:13
@NotMyFault NotMyFault merged commit 7b8c789 into main Mar 27, 2024
11 checks passed
@NotMyFault NotMyFault deleted the fix/no-axis-pattern-race-conditions branch March 27, 2024 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bugfix This PR fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants