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

Fatal Python error: Segmentation fault caused by Pygame #2694

Open
Yyote opened this issue Jan 26, 2024 · 3 comments
Open

Fatal Python error: Segmentation fault caused by Pygame #2694

Yyote opened this issue Jan 26, 2024 · 3 comments
Labels
bug Not working as intended

Comments

@Yyote
Copy link

Yyote commented Jan 26, 2024

Environment:

  • Operating system: Ubuntu 20.04 LTS
  • Python version: 3.8.10
  • SDL version: 2.28.5
  • pygame-ce version: 2.4.0 (just installed from pip)
  • Relevant hardware: -

Current behavior:

This issue has appeared in both original pygame and in pygame-ce.

I have a game in which some cars navigate the environment with obstacles using sensor data and they collide quite frequently with each other and with obstacles. The problem is that sometimes during collisions the program crashes. It throws Fatal Python error: Segmentation fault but I'm 99% sure, that it's pygame. I use mask.overlap() function to detect collisions and when I remove parts that use this function, the program works perfectly fine. What's interesting but very confusing is that the trace points to the blit() function. Maybe I should also mention that I rotate the sprites using rotozoom() and create masks from rotated images.

Expected behavior:

Pixel-perfect sprite collisions using mask.overlap() without getting segmentation faults.

Steps to reproduce:

Create a number of moving sprites and check collisions between them using the mask.overlap() function.

Unfortunately, I cannot easily provide an adequate path to reproduce the issue as I am not sure what really causes it (except that it's part of pygame). If you are really interested, I can provide the repo with the code of my program, but it also uses ROS2 and some of its additional packages so you will be required to download them. Then the steps to reproduce are:

  1. Launch the program
  2. Use arrows to move and collide with objects
  3. Try to collid with objects until it crashes

Stack trace/error output/other error logs

Fatal Python error: Segmentation fault

Current thread 0x00007f504d0a7740 (most recent call first):
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/custom_utils/object_tools.py", line 71 in draw
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/custom_utils/robots.py", line 103 in call_sensors
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/custom_utils/robots.py", line 108 in draw
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/custom_utils/gametools.py", line 141 in draw_every_sprite_in_list
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/main.py", line 94 in main
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/gr_kinematic_sim/sim", line 11 in <module>
Fatal Python error: (pygame parachute) Segmentation Fault
Python runtime state: initialized

Current thread 0x00007f504d0a7740 (most recent call first):
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/custom_utils/object_tools.py", line 71 in draw
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/custom_utils/robots.py", line 103 in call_sensors
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/custom_utils/robots.py", line 108 in draw
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/custom_utils/gametools.py", line 141 in draw_every_sprite_in_list
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/python3.8/site-packages/gr_kinematic_sim/main.py", line 94 in main
  File "/home/leev/workspaces/gr-kinematic-sim/install/gr_kinematic_sim/lib/gr_kinematic_sim/sim", line 11 in <module>
free(): invalid pointer

The mentioned draw() function:

    def draw(self):
        rect_to_draw = copy(self.rect)
        rect_to_draw.x += self.curr_offset_x
        rect_to_draw.y += self.curr_offset_y
        rotated_image = pygame.transform.rotozoom(self._original_image, self._current_rotation, 1)
        self.screen.blit(rotated_image, rect_to_draw)
@Yyote Yyote added the bug Not working as intended label Jan 26, 2024
@Yyote Yyote changed the title Fatal Python error: Segmentation fault cause by Pygame Fatal Python error: Segmentation fault caused by Pygame Jan 26, 2024
@Starbuck5
Copy link
Member

Thanks for your report.

Without a reproducible script it's very hard to make any progress against a segfault. You mentioned a repo?

Your traceback has thread identifiers in it? Are you calling into pygame-ce from multiple threads? That's a dicey prospect, and could explain a segfault.

@Yyote
Copy link
Author

Yyote commented Apr 1, 2024

I will try to provide something but as for now I cannot give access to the repo. But I found out, that the segfault is caused by the linear_velocity variable I use that for some reason became nan. After adding nan to rect coordinates and trying to draw a sprite in that position it resulted in segfault. I don't know, why it happend, but checking it with math.isnan() solved the issue for me. So most probably it is my fault and not pygame's

@Yyote Yyote closed this as completed Apr 1, 2024
@oddbookworm
Copy link
Member

I’m going to reopen this for now. Regardless of the cause, if it’s actually a segfault happening in pygame-ce, it should be fixed on our end. No input should be so broken that we segfault

@oddbookworm oddbookworm reopened this Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Not working as intended
Projects
None yet
Development

No branches or pull requests

3 participants