Hang on SDL_VideoQuit

I have an SDL application, which sometimes hangs on cleanup, specifically on video cleanup on X11_VideoQuit. Here’s the stack of the hang:

#0 0x0012d422 in __kernel_vsyscall ()
#1 0x003ac015 in pthread_cond_wait@@GLIBC_2.3.2 () at …/nptl/sysdeps/unix/sysv/linux/i386/i686/…/i486/pthread_cond_wait.S:122
#2 0x01059dba in ?? () from /usr/lib/libxcb.so.1
#3 0x0105b9b2 in xcb_wait_for_reply () from /usr/lib/libxcb.so.1
#4 0x0021ff96 in _XReply () from /usr/lib/libX11.so.6
#5 0x00213847 in XSync () from /usr/lib/libX11.so.6
#6 0x00185b25 in ?? () from /usr/lib/libSDL-1.2.so.0
#7 0x0018e0fb in ?? () from /usr/lib/libSDL-1.2.so.0
#8 0x0017cc10 in SDL_VideoQuit () from /usr/lib/libSDL-1.2.so.0
#9 0x001546e3 in SDL_QuitSubSystem () from /usr/lib/libSDL-1.2.so.0
#10 0x0015477e in SDL_Quit () from /usr/lib/libSDL-1.2.so.0
#11 0x0804c3e6 in finalize_exec () at client.c:268
#12 0x00e451bf in __run_exit_handlers (status=0, listp=0xf6c324, run_list_atexit=true) at exit.c:78
#13 0x00e4522f in *__GI_exit (status=0) at exit.c:100
#14 0x0804f50c in do_ui_events () at client.c:1964
#15 0x08051c09 in main (argc=2, argv=0xbffff3b4) at client.c:2791

This happens after I close my application using the “X” button.
I initialise sdl with: SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE), and the application works fine, image shows normally.

Looked deeper in the xcb code and found that xcb_wait_for_reply calls _xcb_lock_io which calls pthread_cond_wait, which hangs.

void _xcb_lock_io(xcb_connection_t *c)
if(pthread_equal(c->xlib.thread, pthread_self()))
pthread_cond_wait(&c->xlib.cond, &c->iolock);

Has anyone seen a similar hang? Any suggestion how to fix or w/o this one?
I use:
libsdl1.2-dev which comes with Ubuntu 10.04

I think I found what causes this hang. I forgot to mention that my application had 2 sdl threads, one which handles events and another for rendering. On exit, I’ve called pthread_cancel on the drawing thread, which probably sometimes caused X to be in unstable state, and that’s why XSync would hang in the main thread.
I’ve changed this to let my rendering thread finish gracefully, and now this hang no longer occurs.
Maybe calling XInitThreads would have solved that as well.