GDK BadAccess error when opening window for first time inside docker

I have a docker image with opencv installed via apt. When I run my program it crashes when opening an opencv window with cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL) resulting in the following error:

(python3:218): Gdk-ERROR **: 15:41:23.282: The program 'python3' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadAccess (attempt to access private resource denied)'.
  (Details: serial 287 error_code 10 request_code 130 (MIT-SHM) minor_code 1)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the GDK_SYNCHRONIZE environment
   variable to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)

When I run the program again it works without problems. However, every time I start the docker new again I get this error on the first try. Further tries usually work without problems.

Any help would be much appreciated.

It could be that during the first run, Docker hadn’t initialized its environment before your window call - of course it probably should have.

I would try adding a delay of couple of seconds in the beginning of the program to give Docker time to settle

docker + GUI/graphical issue.

try replicating this with the “xclock” program. if that also fails in this manner, you’ve got a reduced MRE that no longer needs OpenCV.

This didn’t solve the problem, but it has lead me to realise the error is not necessarily originating from the cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL) command. Rather, the error appears only when the image is shown with: cv2.waitKey(100).

So if this is not an opencv problem, where should I ask about this?

xclock works straight away without problems

that just spins the gui event loop. that probably uses GTK. see if you can run any GTK-using programs in your container.

xclock, afaik, talks directly to the X server. if that works, we know that part of the graphics stack works at least.

Alright, so I installed gedit which uses GTK and it has the same problem. It crashes the first time it is run with the GDK BadAccess Error. So I guess the problem lies with the GDK installation and docker.

So I managed to solve the issue for anyone else reading this thread. The solution was to add the --ipc host flag when running the docker image as described here: attempt to access private resource denied · Issue #21 · osrf/docker_images · GitHub

With this it worked on the first try

1 Like