Bug 78140 - The tutorial should have a section on error handling
Summary: The tutorial should have a section on error handling
Status: RESOLVED MOVED
Alias: None
Product: XCB
Classification: Unclassified
Component: Docs (show other bugs)
Version: unspecified
Hardware: Other All
: medium normal
Assignee: xcb mailing list dummy
QA Contact: xcb mailing list dummy
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-05-01 01:15 UTC by Steven Stewart-Gallus
Modified: 2019-02-16 19:40 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Attachments

Description Steven Stewart-Gallus 2014-05-01 01:15:04 UTC
The tutorial briefly mentions it in a few points but doesn't really give a good explanation. This is especially confusing because XCB has two kinds of errors. Errors that the X server sends the program (for example, the client violates the protocol or the server runs out of memory) and errors that the program generates internally (for example, an out of memory error or some low level operating system socket system call generates an IO error).

GUI code is flakey in general and I already run my GUI in a separate process to protect against program exits in library code and corruption but I'd still like to do some obvious error checking.
Comment 1 Uli Schlachter 2014-05-01 09:47:57 UTC
Uhm. Error replies from the server should be explained sufficently, I guess (the whole deal with "checked vs unchecked errors").

For out of memory errors or I/O errors, the xcb_connection_t goes into an error state. All function calls with it will fail and xcb_connection_has_error() can tell you which error occured (there are some defines in xcb.h with a XCB_CONN_ prefix).

XCB never calls exit().

Would it be too much work for you to create a patch for the tutorial that adds what you would have wanted to know? It should be available in git at https://secure.freedesktop.org/cgit/xcb/ and clonable fromgit://wiki.freedesktop.org/wiki/xcb.
Comment 2 Steven Stewart-Gallus 2014-05-01 15:17:43 UTC
Okay, this was confusing for me. There is no section in the tutorial but there is somewhat of an explanation on error handling in the xcb-requests.3 man page (it doesn't mention the xcb_connection_has_error part though).

So, my bug should now be: add a section on error handling to the tutorial, copy some parts of the xcb-requests.3 man page to the section and add information on handling client errors with xcb_connection_has_error.

I should be able to put together at least a prototype myself but I need to ask a few more questions.
Do I need to call xcb_connection_has_error after absolutely every function that gets passed a connection?
Is there a function to get an error string for each error (keep in mind that one may want to sometimes localize error messages so this is not as easy as it seems).
How do I check for errors with the xcb_disconnect function (I know that free(3) does not give errors but what about close(2))?
Comment 3 Uli Schlachter 2014-05-01 15:38:57 UTC
(In reply to comment #2)
> I should be able to put together at least a prototype myself but I need to
> ask a few more questions.

Thanks a lot!

> Do I need to call xcb_connection_has_error after absolutely every function
> that gets passed a connection?

Nope. Nothing bad happens when you use an xcb_connection_t that is an error state. Must functions will just return e.g. NULL (however, no one ever expects e.g. xcb_get_setup() to return NULL, so other code can still crash...).

> Is there a function to get an error string for each error (keep in mind that
> one may want to sometimes localize error messages so this is not as easy as
> it seems).

Nope, and I wouldn't want to add one exactly because of things like localization. I guess you are expected to write your own function that translates the ~5 error codes that are currently in-use into a string and that returns something like "unknown error 42" for other values.

E.g. qt does the following:
https://qt.gitorious.org/qt/qtbase/source/d52b00e1d3cc60c81b54a89d6da488dc4bbce384:src/plugins/platforms/xcb/qxcbconnection.cpp#L95-126

> How do I check for errors with the xcb_disconnect function (I know that
> free(3) does not give errors but what about close(2))?

There just are no errors. :-)
Also, there aren't many sensible things that can be done if close(), pthread_mutex_destroy() or pthread_cond_destroy() fail (AFAIR on some systems close() can fail and still have closed the FD already, so retrying would be unsafe in multi-threaded programs anyway).

So if any errors occur during xcb_disconnect(), you'll get a memory / file descriptor leak.
Comment 4 Steven Stewart-Gallus 2014-05-02 02:08:02 UTC
I find it unfortunate that xcb_disconnect does not return an error code.

First of all, the close and EINTR problem is easily solvable. Simply, block all signals while closing a file and then the EINTR error can never happen.

Second of all, an obvious thing to do if close fails in a process is to exit the process and disable that part of the system but keep the rest of the system running (that way someone can run a debug console later on and diagnose and fix the problem or recover data if the system is truely broken). Simply going on instead is wrong because that might result in corrupting user data. I'd even rather have the function abort on errors then simply ignore them.

Thirdly, pthread_mutex_destroy and pthread_cond_destroy should not fail with  errors on proper use of their functionality so they are irrelevant.
Comment 5 GitLab Migration User 2019-02-16 19:40:36 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to freedesktop.org's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.freedesktop.org/xorg/proto/xcbproto/issues/11.


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.