SDL icon indicating copy to clipboard operation
SDL copied to clipboard

Regression: Assertion failed on SDL_DestroyWindow (with example code)

Open oo8dev opened this issue 3 years ago • 4 comments

Reproductible code:

#include <SDL3/SDL.h>

int main() {
  auto *window = SDL_CreateWindow("test", 640, 480, 0);

  auto *surf_renderer = SDL_CreateSurface(640, 480, SDL_PIXELFORMAT_RGB24);
  auto *renderer = SDL_CreateSoftwareRenderer(surf_renderer);

  for (int i = 0; i < 100; i++) {
    auto *screen = SDL_GetWindowSurface(window);
    SDL_BlitSurfaceScaled(surf_renderer, nullptr, screen, nullptr);
    SDL_UpdateWindowSurface(window);
  }

  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
}

g++ file.cpp -lSDL3 -o test && ./test

Assertion: Assertion failure at SDL_LockRWLockForWriting_REAL (/home/aleksander/Downloads/SDL/src/thread/pthread/SDL_sysrwlock.c:69), triggered 1 time: 'rc == 0'

Tested on main branch, newest commit (1c4723ac), Linux, pthreads backend, Wayland

oo8dev avatar Nov 13 '23 14:11 oo8dev

The error code returned by pthread_rwlock_wrlock() is EDEADLK, which happens when the same thread attempts to lock the RWLock that guards the properties hash table more than once. While destroying the window properties, the texture properties are implicitly destroyed, which causes it to recurse into the SDL_DestroyProperties() function resulting in the same thread trying to acquire the RWLock multiple times, and according to the man page "The calling thread may deadlock if at the time the call is made it holds the read-write lock (whether a read or write lock)."

The SRW locks used in the Win32 backend apparently aren't recursion safe either and could run into problems in this scenario.

@slouken The property hash table lock might need to be a mutex instead of an RWLock, since the former is recursion-safe and the latter isn't?

Kontrabant avatar Nov 13 '23 19:11 Kontrabant

Ah good point, do you want to create a PR for this?

slouken avatar Nov 13 '23 19:11 slouken

Will do.

Kontrabant avatar Nov 13 '23 20:11 Kontrabant

Thanks!

slouken avatar Nov 13 '23 22:11 slouken