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

GDScript: Fix lambda hot reloading #81628

Merged
merged 1 commit into from
Oct 20, 2023

Conversation

rune-scape
Copy link
Contributor

@rune-scape rune-scape commented Sep 13, 2023

fixes #62769

swaps out the function pointers used in LambdaCallables when a script is reloaded
if it cant find a good replacement it invalidates the callable, so no more 'Bad Address Index' and crashes, it tells you its called an invalid lambda
the logic it uses to swap lambdas is very simple right now, swapping only if the argument count vaguely matches, and no lambdas were added or removed, evaluated for each function the lambda was in
also should be safe across threads, because lambdas can be created on any thread. i took care to make sure it wouldnt slow lambda creation down

it may be a good idea to add some more logic to how old lamdas are matched to new ones, the data is there but i dont know what would be intuitive, what would break logic, im all ears for heuristics

MRP:
81628.zip

Production edit: added the MRP

@rune-scape rune-scape requested a review from a team as a code owner September 13, 2023 21:39
@rune-scape rune-scape force-pushed the rune-lambda-hotswap branch 3 times, most recently from 5b65e51 to 9cf297a Compare September 13, 2023 22:02
@rune-scape
Copy link
Contributor Author

umm, the failed check seems like an unrelated crash..

@ryanabx
Copy link
Contributor

ryanabx commented Sep 13, 2023

umm, the failed check seems like an unrelated crash..

You're absolutely right

@akien-mga akien-mga added this to the 4.2 milestone Sep 13, 2023
modules/gdscript/gdscript_compiler.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_compiler.cpp Outdated Show resolved Hide resolved
Copy link
Member

@dalexeev dalexeev left a comment

Choose a reason for hiding this comment

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

I'm not qualified enough to review this, I just noticed a couple of minor flaws.

If hot reloading is only available in the editor, does it make sense to wrap some code in TOOLS_ENABLED or DEBUG_ENABLED?

modules/gdscript/gdscript.h Outdated Show resolved Hide resolved
modules/gdscript/gdscript_lambda_callable.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_lambda_callable.cpp Outdated Show resolved Hide resolved
@dalexeev dalexeev requested a review from a team September 14, 2023 11:37
@rune-scape rune-scape force-pushed the rune-lambda-hotswap branch 2 times, most recently from e756e07 to 7b6b2ba Compare September 14, 2023 19:20
@rune-scape
Copy link
Contributor Author

If hot reloading is only available in the editor, does it make sense to wrap some code in TOOLS_ENABLED or DEBUG_ENABLED?

@dalexeev script.reload(true) is hot reloading, i believe. and thats available at runtime with tools disabled
placeholders, docs, and tests are whats disabled in gdscript when the editor is
except for old_static_variables and member_default_values? it seems inconsistent, but hot-reloading in concept is allowed without tools,, and this is a memory safety issue,, so i think a big chunk of it would need to stay, and it should probably be enabled wherever member function hot-reloading is enabled, which is all the time with or without tools right now

tbh i dont know why you would pass false to reload for that matter,,, it seems like that would just cause weird runtime behavior. is it even used anywhere?

@rune-scape
Copy link
Contributor Author

should i click the resolve conversation button now that ive pushed the corrections?? i do not remember

@YuriSizov
Copy link
Contributor

YuriSizov commented Sep 14, 2023

should i click the resolve conversation button now that ive pushed the corrections?? i do not remember

If you've addressed the comment and there are no open matters to discuss, then feel free to click resolve :)

@rune-scape
Copy link
Contributor Author

@AThousandShips id appreciate another review on the thread safety, i changed some things in gdscript.cpp, and i remember your affinity on the subject

@YuriSizov YuriSizov changed the title GDScript: Lambda hot reloading GDScript: Fix lambda hot reloading Sep 15, 2023
Copy link
Member

@AThousandShips AThousandShips left a comment

Choose a reason for hiding this comment

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

Otherwise I don't see any issues with the threading, but I'm a bit rusty with the specifics so might be missing something, so someone else should take a look at that as well

modules/gdscript/gdscript.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_compiler.cpp Outdated Show resolved Hide resolved
@adamscott
Copy link
Member

Thanks @AThousandShips, I integrated your suggestions into the PR as @rune-scape is unavailable for the moment.

@adamscott
Copy link
Member

I created a MRP to make sure that it works. And it works flawlessly.
81628.zip

Just run the project, then change the inner value of the lambdas. It just works!

@adamscott adamscott requested a review from vnen October 6, 2023 12:54
Copy link
Member

@adamscott adamscott left a comment

Choose a reason for hiding this comment

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

It's fine for me. It works and the code is clean. I'm no hot-reloading-whiz, but I'm OK with merging this for 4.2.

@adamscott adamscott force-pushed the rune-lambda-hotswap branch from 560d1b3 to d05aa3b Compare October 6, 2023 14:07
@rune-scape
Copy link
Contributor Author

while using it yesterday i just found a small bug related to thread safety checks. pushing that now before it gets merged

@rune-scape rune-scape force-pushed the rune-lambda-hotswap branch from d05aa3b to f7f3e10 Compare October 6, 2023 23:47
Copy link
Member

@vnen vnen left a comment

Choose a reason for hiding this comment

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

For a glance it looks good. It'll take some time if I dive deep on the logic, so I'll trust rune on this. It's already a big improvement over the current status even if some bug crops up.

modules/gdscript/gdscript_compiler.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_compiler.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_compiler.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_compiler.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_compiler.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript_compiler.cpp Outdated Show resolved Hide resolved
modules/gdscript/gdscript.h Outdated Show resolved Hide resolved
modules/gdscript/gdscript.cpp Outdated Show resolved Hide resolved
Co-authored-by: Adam Scott <ascott.ca@gmail.com>
@adamscott adamscott force-pushed the rune-lambda-hotswap branch from 9a6ed41 to 9fb8862 Compare October 17, 2023 19:53
Copy link
Member

@vnen vnen left a comment

Choose a reason for hiding this comment

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

Looks good. It's a huge improvement over the current status.

@akien-mga akien-mga merged commit aa3beb5 into godotengine:master Oct 20, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks! Amazing work 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lambda references are corrupted when script is modified while debugging
8 participants