Was thread handling changed? Bugged?

Hello all,

I’m not a very experienced programmer, so I apologize if I’m using the wrong lingo here. I noticed lately that FiveM has been handling threads a little differently. Is it just my imagination?

I have a script that draws icons on the screen as well as removes the street names from the bottom right of the screen. A lot of the methods in my code create new threads so the icons don’t disappear. Lately I noticed that there’s a small chance that icons disappear for a split second anyway, and the street names reappear too.

I even went back to old versions of the code that I know didn’t produce this error and it still happened. I also noticed that the popular speedometer script sometimes shows the needle and lights BEHIND the background at random. That never happened to me until recently.

Does anyone know if a recent update could have done this? Or whether it’s a server or client update? I would imagine something introduced a small delay when it comes to threads, forcing the script to hang for an instant? I really have no idea what it could be.

Thanks,

  • M

Do you happen to have any example code/repro? It might be some resources depended on an implementation detail of the thread scheduling system and created excessive amounts of new coroutines for ‘every single tiny thing’ where the coroutines should have been merged as a single one.

1 Like

Thank you for your reply. I’m still trying to isolate the exact cause so it’s going to be a while before I can show you any code.

In the meantime, if I can make a suggestion, I say try either the Initial D6 or D7 skins for the speedometer script (link below). Sometimes the needle appears behind the background (the needle is drawn by a different .lua script). I have to repeatedly restart the resource on my server until it shows up properly client side. Obviously that’s bad if there are other players.

It was never an issue before now so could it be related?

I’ll post back here when I have some code ready.

Ouch. Yeah, no, there never were any guarantees about resource/thread/script order, but they were deterministic before; so this specific script only worked by coincidence (any tiny change and it’d lead to the order being the other way around).

Generally this should’ve been implemented using a callback in this skin configuration file, not by spawning a separate coroutine that may or may not run in the right order…

1 Like

Strange how that only started happening recently though. I would have noticed it before. I never bothered to update it on my server so nothing about the speedometer was changed in almost a year.

Maybe I’m just doing something wrong. The code I’m having trouble with looks something like this:

Citizen.CreateThread(function()
     while true do
          Citizen.Wait(0)
          //Draw icons using DrawSprite()
          //Hide vanilla GTA street names this frame
     end
end)

Citizen.CreateThread(function()
     while true do
          Citizen.Wait(0)
          if IsControlJustPressed(1, 173) then 
               ChangeSomething()
          end
     end
end)

function ChangeSomething()
     Citizen.CreateThread(function()     //I think this is when the flicker happens
          //Stuff happens
          Citizen.Wait(1000)
          //Stuff happens
     end)
end

This runs ChangeSomething() when you press DOWN. It works just fine functionally but about 1/6th of the time, the icons disappear and the street names are shown for a single frame. Sometimes the icons get brighter as if being drawn twice. To me it seems that the entire resource hangs for a split second.

In my case it’s just a minor nuisance, but I feel like this could cause issues down the line so I just wanted to put it out there, unless of course I’m doing something wrong here.

If you prefer to see the real code (it’s pretty big and hard to follow) I’ll PM it to you.

Yes, this should’ve been fixed as of the latest production push - intermittently, creating threads during execution of another thread would’ve led to thread execution order changing during execution due to a detail of how Lua tables work.

The other issue is a bit more confusing to fix and might lead to some very weird bookkeeping details having to be reintroduced, while they were removed to fix another bug some individuals were experiencing.

2 Likes

Confirmed, I don’t get the flicker anymore, and the speedometer seems to be working fine as it is.

Thank you for the insight and good job with the fix!