Skip to content

Commit

Permalink
fix: SIGPIPE handling with FrankenPHP
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Mar 24, 2024
1 parent 9af3c45 commit 91cbd89
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
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

0 comments on commit 91cbd89

Please sign in to comment.