Navigation Menu

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

Fix crashes when some external APIs fail #2700

Closed
wants to merge 2 commits into from
Closed

Fix crashes when some external APIs fail #2700

wants to merge 2 commits into from

Conversation

ZhouyangJia
Copy link
Contributor

Hi,

I analyzed the collectd source code and found some potential API bugs that may cause crashes. These crashes are mainly caused by insufficient error handling of API functions like PyType_Ready or pthread_create.

I think it's unsafe to assume the library function would be correct. It would be better if we could handle the error properly (at least printing a message).

Best,
Zhouyang

The API PyType_Ready may fail, when it happens, the program may crash.
Print error log message when pthread_create fails.
@dago
Copy link
Contributor

dago commented Feb 26, 2020

@ZhouyangJia Would you mind rebasing the code resolving the conflicts and adjust to individual error messages? I would like to include this PR in the upcoming 5.11.0.

@dago dago added this to the 5.11.0 milestone Feb 26, 2020
PyType_Ready(&ConfigType);
PyType_Ready(&PluginDataType);
if (PyType_Ready(&ConfigType) < 0) {
cpy_log_exception("python initialization");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the below error messages are all the same, this makes debugging harder than necessary, please change these to include the actual failure in the error message so each error can be tracked to the respective line.

@dago dago added the Waiting for response - 1st time Waiting for contributor to respond - 1st call label Feb 29, 2020
@dago dago modified the milestones: 5.11.0, 5.12.0 Mar 2, 2020
@ZhouyangJia
Copy link
Contributor Author

Hi, I add individual error messages in the error-handling code in #3402.
The code related to the fix in src/daemon/collectd.c has been deleted, so I remove that part.

Thanks

@@ -71,7 +71,8 @@ static void sig_usr1_handler(int __attribute__((unused)) signal) {
* so it should be done asynchronously */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&thread, &attr, do_flush, NULL);
if (pthread_create(&thread, &attr, do_flush, NULL) != 0)
fprintf(stderr, "Can not create thread: %s.\n", strerror(errno));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point we're already forked so stderr is closed.
You're also using strerror() which is not async signal safe.

I don't think handling the error buys us anything here.

@@ -1257,16 +1257,34 @@ static int cpy_init_python(void) {
Py_Initialize();
python_sigint_handler = PyOS_setsig(SIGINT, cur_sig);

PyType_Ready(&ConfigType);
PyType_Ready(&PluginDataType);
if (PyType_Ready(&ConfigType) < 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PyType_Ready returns -1 on failure, please compare it with -1 directly.

@ZhouyangJia ZhouyangJia closed this Mar 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Waiting for response - 1st time Waiting for contributor to respond - 1st call
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants