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 uint64 underflow in tick_busy_loop #2466

Merged

Conversation

oddbookworm
Copy link
Member

It was reported on discord that using tick_busy_loop could cause a crash in a specific scenario.

Reproducer

import pygame


WIDTH, HEIGHT = 640, 360
FPS = 60

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.Clock()

running = True
while running:
    dt = clock.tick_busy_loop(FPS) / 1000
    screen.fill("black")
    
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            running = False

    pygame.display.flip()

It's an odd one though, I didn't see it on my own machine. After some troubleshooting with @Matiiss and the reporter, I believe it is due to an int underflow when there is a lag spike in cpu processing, causing delay to become insanely large (thus blocking the while loop from ever finishing).

I don't think a regression test is possible with our current framework

@oddbookworm oddbookworm added time pygame.time bugfix PR that fixes bug labels Sep 24, 2023
@oddbookworm oddbookworm requested a review from a team as a code owner September 24, 2023 02:51
src_c/time.c Outdated Show resolved Hide resolved
src_c/time.c Outdated Show resolved Hide resolved
Copy link
Member

@MyreMylar MyreMylar left a comment

Choose a reason for hiding this comment

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

LGTM 👍

Copy link
Member

@Matiiss Matiiss left a comment

Choose a reason for hiding this comment

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

LGTM, the window doesn't hang anymore

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks 🎉

@ankith26 ankith26 merged commit 893e653 into pygame-community:main Sep 26, 2023
31 of 32 checks passed
@ankith26 ankith26 added this to the 2.4.0 milestone Sep 26, 2023
@oddbookworm oddbookworm deleted the fix_tick_busy_loop_for_slow_cpus branch December 21, 2023 03:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix PR that fixes bug time pygame.time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants