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: Combobox ConVar change callback deletion #1075

Merged
merged 2 commits into from
Oct 16, 2023

Conversation

saibotk
Copy link
Member

@saibotk saibotk commented Oct 16, 2023

Fixes #1068

We need to remove the callback in a timer, because otherwise the ConVar change callback code
will throw an error while looping over the callbacks.
This happens, because the callback is removed from the same table that is iterated over.
Thus, the table size changes while iterating over it and leads to a nil callback as the last entry.

See https://github.com/Facepunch/garrysmod/blob/1b512930d1f8fb1acf6235e584c4ec1ff84e9362/garrysmod/lua/includes/modules/cvars.lua#L44
and
https://github.com/Facepunch/garrysmod/blob/1b512930d1f8fb1acf6235e584c4ec1ff84e9362/garrysmod/lua/includes/modules/cvars.lua#L97

here the code uses table.Remove, which causes the table to be reindexed and resized, this is the root cause for the problem.
We should thus avoid removing callbacks in the callback itself.

Fixes #1068

We need to remove the callback in a timer, because otherwise the ConVar change callback code
will throw an error while looping over the callbacks.
This happens, because the callback is removed from the same table that is iterated over.
Thus, the table size changes while iterating over it and leads to a nil callback as the last entry.

See https://github.com/Facepunch/garrysmod/blob/1b512930d1f8fb1acf6235e584c4ec1ff84e9362/garrysmod/lua/includes/modules/cvars.lua#L44
and
https://github.com/Facepunch/garrysmod/blob/1b512930d1f8fb1acf6235e584c4ec1ff84e9362/garrysmod/lua/includes/modules/cvars.lua#L97

here the code uses table.Remove, which causes the table to be reindexed and resized, this is the root cause for the problem.
We should thus avoid removing callbacks in the callback itself.
@saibotk saibotk removed the request for review from ZenBre4ker October 16, 2023 22:10
Copy link
Member

@Histalek Histalek left a comment

Choose a reason for hiding this comment

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

LGTM

@ZenBre4ker
Copy link
Contributor

Interesting and makes sense.
Gonna approve tomorrow and take a closer look.
Thanks :D

@TimGoll TimGoll merged commit b0873db into master Oct 16, 2023
4 checks passed
@TimGoll TimGoll deleted the fix-combobox-convar-removal branch October 16, 2023 22:11
@saibotk
Copy link
Member Author

saibotk commented Oct 16, 2023

Too late tim was here already

@TimGoll
Copy link
Member

TimGoll commented Oct 16, 2023

Interesting and makes sense. Gonna approve tomorrow and take a closer look. Thanks :D

well it is already merged :D

@NickCloudAT
Copy link
Contributor

I would still like to comment on this:

I did think about using a timer, but I decided against it as I though 0 tick timers are an "ugly" solution.
Also: This PR does not fix the language % calculations in the F1 menu. It's still takes % of the old language set.

The "oldValue" was merely a nice to have in my PR I guess as you may want the previous value directly from the combobox and not have to get the conVar first, which is more CPU intensive as stated in the wiki.

@saibotk
Copy link
Member Author

saibotk commented Oct 17, 2023

Yes, this is only a fix for the callbacks themselves.
I do not think that this is particularly ugly in this context, but rather i think of this whole functionality as conceptually flawed and would rather replace this sooner than later with something that does not need to remove itself.

But please open up a new PR for the language fix, if you can spare the time! Id love to have that one in too.

@TimGoll
Copy link
Member

TimGoll commented Oct 17, 2023

Reagding the uglyness: In my opinion are 0 second timer fine, they are a solid way to make sure something is executed in the next tick

@NickCloudAT
Copy link
Contributor

Yes, this is only a fix for the callbacks themselves. I do not think that this is particularly ugly in this context, but rather i think of this whole functionality as conceptually flawed and would rather replace this sooner than later with something that does not need to remove itself.

But please open up a new PR for the language fix, if you can spare the time! Id love to have that one in too.

I give you that the concept is probably flawed, yes. Maybe it's just my concept of "ugly" that makes it weird in my mind.

If I get to fix the lang thingy: Should I maybe still add the "oldValue" too? This still gives advantages I think.

@saibotk
Copy link
Member Author

saibotk commented Oct 17, 2023

What advantage exactly? I did not really see any need for it, but maybe i just did not get it correctly.

@NickCloudAT
Copy link
Contributor

What advantage exactly? I did not really see any need for it, but maybe i just did not get it correctly.

Maybe I'm just dumb, but I did not see another way of getting the old value except for reading out the conVar value itself. Which would be with "GetConVar". The GMOD-Wiki to that states:
"Due to this function using GetConVar_Internal internally it tends to be relatively slow. Please attempt to 'cache' the return of what you used to make it instead of using this function."

I mean in daily use this would probably not make that much of a difference, but IMO giving back the "oldValue" in the function itself is cleaner then needing to get it from the convar.
Also in my PR I put the "onSelect" AFTER the convar gets set, which in turn makes it impossible to get the old value of the convar but in turn would automatically fix the language % error as the VGUI refresh (at least IMO) should happen after a convar is changed, not before.

@EntranceJew
Copy link
Contributor

The wiki's comment is out of date, in the code itself on the FP repo you can see it is caching the reference to the convar

@saibotk
Copy link
Member Author

saibotk commented Oct 17, 2023

"Performance" is super negligible here as its not called often, but sure go ahead and fix the language percentage.

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.

Language selection error
6 participants