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: SIGPIPE handling with FrankenPHP #550

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions amqp_connection_resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#else
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#endif

#if HAVE_LIBRABBITMQ_NEW_LAYOUT
Expand Down Expand Up @@ -689,15 +690,21 @@ static void connection_resource_destructor(amqp_connection_resource *resource, i
assert(resource != NULL);

#ifndef PHP_WIN32
void *old_handler;

/*
If we are trying to close the connection and the connection already closed, it will throw
SIGPIPE, which is fine, so ignore all SIGPIPES
*/

struct sigaction oldact;
struct sigaction act = { 0 };

act.sa_flags = SA_ONSTACK;
act.sa_handler = SIG_IGN;

/* Start ignoring SIGPIPE */
old_handler = signal(SIGPIPE, SIG_IGN);
if (sigaction(SIGPIPE, &act, &oldact) == -1) {
perror("sigaction");
}
#endif

if (resource->parent) {
Expand All @@ -720,7 +727,11 @@ static void connection_resource_destructor(amqp_connection_resource *resource, i

#ifndef PHP_WIN32
/* End ignoring of SIGPIPEs */
signal(SIGPIPE, old_handler);
oldact.sa_flags |= SA_ONSTACK;

if (sigaction(SIGPIPE, &oldact, NULL) == -1) {
perror("sigaction");
}
#endif

pefree(resource, persistent);
Expand Down
23 changes: 16 additions & 7 deletions amqp_exchange.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#else
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#endif
#if HAVE_LIBRABBITMQ_NEW_LAYOUT
#include <rabbitmq-c/amqp.h>
Expand Down Expand Up @@ -466,11 +467,6 @@ static PHP_METHOD(amqp_exchange_class, publish)
zend_long flags = AMQP_NOPARAM;
bool flags_is_null = 1;

#ifndef PHP_WIN32
/* Storage for previous signal handler during SIGPIPE override */
void *old_handler;
#endif

amqp_basic_properties_t props;

if (zend_parse_parameters(
Expand Down Expand Up @@ -625,7 +621,16 @@ static PHP_METHOD(amqp_exchange_class, publish)

#ifndef PHP_WIN32
/* Start ignoring SIGPIPE */
old_handler = signal(SIGPIPE, SIG_IGN);
struct sigaction oldact;
struct sigaction act = { 0 };

act.sa_flags = SA_ONSTACK;
act.sa_handler = SIG_IGN;

/* Start ignoring SIGPIPE */
if (sigaction(SIGPIPE, &act, &oldact) == -1) {
perror("sigaction");
}
#endif

zval *exchange_name = PHP_AMQP_READ_THIS_PROP("name");
Expand All @@ -650,7 +655,11 @@ static PHP_METHOD(amqp_exchange_class, publish)

#ifndef PHP_WIN32
/* End ignoring of SIGPIPEs */
signal(SIGPIPE, old_handler);
oldact.sa_flags |= SA_ONSTACK;

if (sigaction(SIGPIPE, &oldact, NULL) == -1) {
perror("sigaction");
}
#endif

if (status != AMQP_STATUS_OK) {
Expand Down