#Python randomly changing values [bug?]

1 messages ยท Page 1 of 1 (latest)

hollow kayak
#

Python randomly changing values [bug?]

distant prawn
#

you may be dealing with race conditions

hollow kayak
#

I don't wanna deal with assumptions.

distant prawn
#

is your bot used often?

hollow kayak
#

My bot is only used in one Discord Server and used by 2-3 people at a time.

#

The games tend to work fine but then randomly the bug happens as mentioned above and shown in the screenshot.s

#

I've read all through my code and I don't see how it's possible for Python to be messing this up as mathmatically it isn't possible.

#

I have no idea where it's getting the numbers from or anything.

copper sinew
#

tbf, does look like race conditions. Doesn't really depend how many users they have. If they are handled at the same time, same variable or so, it could occur

#

always need to make it threadsafe in this case

#

(eg. using locks)

hollow kayak
#

I'm not understanding?

#

Can you look at my code and tell me if it's anything in there? I don't think it is cause the code doesn't seem like it'd be able to do that.

#

Also I don't know what you mean by race conditions.

#

Nor how to prevent it.

crude lichen
#

did you verify the contents of the json file are right?

hollow kayak
#

The Json Database is fine no issues with it.

distant prawn
#

also your push function has a little error

#

it changes the balance of the player

hollow kayak
hollow kayak
#

Push isn't an issue push works fine.

distant prawn
#
async def push(self, bust = False):       
        cas = self.cas
        money = "{:,}".format(cas["money"]) #on push they don't win nor lose anything
        embed = discord.Embed(title="You Pushed [Blackjack]",description=f"**Game ID**: `{self.id_value}`\n*Use `/lookup` to view this game.*",color=p84clr)
        embed.add_field(name=f"Player [{self.player.value()}]",value=f"{self.player}")
        embed.add_field(name=f"Dealer [{self.dealer.value()}]",value=f"{self.dealer}")
        embed.add_field(name="Profit",value=f"`$0`", inline=False)
        embed.add_field(name="Balance",value=f"`${money}`")
        if self.message:
            await self.message.edit(None, embed = embed, view = None)
        self.changeBalance(self.owner, round(self.bet)) # i believe this isnt supposed to be here```
hollow kayak
#

Ye

#

it doesn't change their balance tho

#

Since byAmount is 0

#

It just sets it to their money.

#

Not the issue nor the focus.

distant prawn
#

ah whatever

hollow kayak
#

It's only happening when they win / lose blackjack/crash

#

Which is weird lol

distant prawn
#

race condition is basically when two threads for instance try to write to the same variable for instance

#

only one of the two threads gets their result saved permanently afterwards

hollow kayak
#

hm

#

How would that happen if I have a 5 second cooldown on all commands?

#

Plus an in-game prevention system so no duel games happen.

distant prawn
#

very unlikely but you should still check

hollow kayak
#

I did check I see nothing?

#

I legit see no issues.

#

But yet there's an issue.

distant prawn
#

this ones a toughie ยฏ_(ใƒ„)_/ยฏ

hollow kayak
#

Yup.

#

So what do I do to fix it?.

#

I've tested it myself and there's no explanation other to say it randomly just happens.

#

It happens even if you don't spam it. I took a 30 min break, came back.. happened.

#

So I don't understand.

#

Anyone know how to fix it ๐Ÿ˜ฆ

copper sinew
#

I told you, locks

#

Pardon the Google URL, am on the phone

#

Just search for python thread safety and python locks

hollow kayak
#

How do you even know thats the issue

still plover
#

just to confirm the issue is related to the json and database?

copper sinew
#

Educated guess after years of professional python development ... You got a condition going on where variables are being changed in between start and end

#

It's the only explanation

crude lichen
copper sinew
#

Even if it is in the JSON dB, you got to make it safe for concurrency

crude lichen
hollow kayak
#

Would it be what Fishy said being the lock issue?

hollow kayak
#

Hello?

#

Anyone?

hollow kayak
#

Bruh locking isn't rlly explained anywhere and that isn't even the issue I was having.

crude lichen
#

so you fixed it?

#

@hollow kayak

#

oh they left...

crude needle
#

F

crude lichen
#

and joined and left again ๐Ÿ™ƒ

hollow kayak
willow holly
#

miner mends is kinda dumb tbf

fading sable
#

๐Ÿ’€