Skip to content

Set max_methods for lock and unlock to 1#60803

Merged
KristofferC merged 1 commit intoJuliaLang:masterfrom
JamesWrigley:lock-maxmethods
Jan 30, 2026
Merged

Set max_methods for lock and unlock to 1#60803
KristofferC merged 1 commit intoJuliaLang:masterfrom
JamesWrigley:lock-maxmethods

Conversation

@JamesWrigley
Copy link
Copy Markdown
Member

This should prevent invalidations, particularly in IJulia:

 inserting lock(io::IJulia.IJuliaStdio) @ IJulia ~/git/IJulia.jl/src/stdio.jl:17 invalidated:
   backedges: 1: superseding lock(::IO) @ Base io.jl:33 with MethodInstance for lock(::IO) (150 children)

 inserting unlock(io::IJulia.IJuliaStdio) @ IJulia ~/git/IJulia.jl/src/stdio.jl:18 invalidated:
   backedges: 1: superseding unlock(::IO) @ Base io.jl:34 with MethodInstance for unlock(::IO) (2275 children)

See for example: JuliaLang/IJulia.jl#1192 (comment)

This should prevent invalidations, particularly in IJulia.
@nsajko
Copy link
Copy Markdown
Member

nsajko commented Jan 24, 2026

xref PR #59377

@JamesWrigley
Copy link
Copy Markdown
Member Author

(bump)

end

lock(::IO) = nothing
typeof(lock).name.max_methods = UInt8(1)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What happens if a method of lock gets defined and a method instance gets compiled/inferred before you change max_methods here? Would it cause miscompilation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

From grepping the code and looking at the include order in Base.jl I think this is where the function is declared the first time, so that should not happen.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if some other PR switches the code around a bit, sometime in the future? Will we get miscompilation silently?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems like a separate concern from this PR since this code pattern is already used in other places already. Maybe move the discussion to a separate issue?

Copy link
Copy Markdown
Member

@nsajko nsajko Jan 30, 2026

Choose a reason for hiding this comment

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

I don't think it is a separate issue. The pattern of modifying max_methods for a function is only used for a few examples currently:

So if it becomes common to modify max_methods for various functions all over the code base, I think then we would actually need to start worrying about stuff like this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, since it is already in the code base, a dedicated issue makes more sense rather than comments on a PR.

@KristofferC KristofferC merged commit 06d1008 into JuliaLang:master Jan 30, 2026
10 checks passed
@JamesWrigley JamesWrigley deleted the lock-maxmethods branch January 30, 2026 14:18
@JamesWrigley
Copy link
Copy Markdown
Member Author

Any chance of this getting backported to 1.13? Would be really nice to have those invalidations fixed in IJulia.

@JamesWrigley JamesWrigley added the backport 1.13 Change should be backported to release-1.13 label Feb 8, 2026
@JamesWrigley
Copy link
Copy Markdown
Member Author

I took the liberty of adding the backport label, but feel free to remove it if it's too big a change.

KristofferC pushed a commit that referenced this pull request Mar 3, 2026
@KristofferC KristofferC mentioned this pull request Mar 3, 2026
56 tasks
@KristofferC KristofferC removed the backport 1.13 Change should be backported to release-1.13 label Mar 13, 2026
@KristofferC KristofferC added the backport 1.12 Change should be backported to release-1.12 label Apr 1, 2026
KristofferC pushed a commit that referenced this pull request Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 1.12 Change should be backported to release-1.12 invalidations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants