Skip to content

Commit 8e29999

Browse files
Mathewsplitice
authored andcommitted
tests fix
1 parent 205097c commit 8e29999

16 files changed

Lines changed: 536 additions & 140 deletions

.github/workflows/build-test.yml

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ jobs:
4848
target:
4949
- build: Release
5050
asset: release
51+
cow_suppressions: true
5152
- build: Debug
5253
asset: debug
54+
cow_suppressions: true
5355
- build: Debug
5456
asset: debug-debug-no-copy-on-write
57+
cow_suppressions: false
5558
steps:
5659
- name: Checkout
5760
uses: actions/checkout@v2
@@ -102,9 +105,13 @@ jobs:
102105
run: |
103106
PWD=$(pwd)
104107
chmod +x ./tests/tests ./src/server/scache
108+
VALGRIND_SUPPRESSIONS=""
109+
if [[ "${{ matrix.target.cow_suppressions }}" == "true" ]]; then
110+
VALGRIND_SUPPRESSIONS="--suppressions=$PWD/valgrind-cow-child.supp"
111+
fi
105112
106113
sudo apt-get install valgrind
107-
valgrind --leak-check=full --show-leak-kinds=all ./tests/tests $PWD/src/server/scache $PWD/testcases/
114+
valgrind --leak-check=full --show-leak-kinds=all $VALGRIND_SUPPRESSIONS ./tests/tests $PWD/src/server/scache $PWD/testcases/
108115
tests_rbuf:
109116
runs-on: ubuntu-latest
110117
needs: [build]
@@ -114,10 +121,13 @@ jobs:
114121
target:
115122
- build: Release
116123
asset: release
124+
cow_suppressions: true
117125
- build: Debug
118126
asset: debug
127+
cow_suppressions: true
119128
- build: Debug
120129
asset: debug-debug-no-copy-on-write
130+
cow_suppressions: false
121131
steps:
122132
- name: Checkout
123133
uses: actions/checkout@v2
@@ -172,6 +182,11 @@ jobs:
172182
sudo mkdir -p /var/lib/scache/
173183
sudo chmod 0777 /var/lib/scache/
174184
chmod +x ./tests/tests ./src/server/scache
185+
PWD=$(pwd)
186+
VALGRIND_SUPPRESSIONS=""
187+
if [[ "${{ matrix.target.cow_suppressions }}" == "true" ]]; then
188+
VALGRIND_SUPPRESSIONS="--suppressions=$PWD/valgrind-cow-child.supp"
189+
fi
175190
176191
sudo mkdir -p /var/lib/scache/ &
177192
sudo apt-get install psmisc valgrind
@@ -196,7 +211,7 @@ jobs:
196211
197212
query &
198213
199-
timeout --kill-after=5 --preserve-status 2m valgrind --leak-check=full --show-leak-kinds=all src/server/scache -b 127.0.0.1:8081 -B 127.0.0.1:8082
214+
timeout --kill-after=5 --preserve-status 5m valgrind --leak-check=full --show-leak-kinds=all $VALGRIND_SUPPRESSIONS src/server/scache -b 127.0.0.1:8081 -B 127.0.0.1:8082
200215
tests_php_api:
201216
runs-on: ubuntu-latest
202217
needs: [build]
@@ -206,10 +221,13 @@ jobs:
206221
target:
207222
- build: Release
208223
asset: release
224+
cow_suppressions: true
209225
- build: Debug
210226
asset: debug
227+
cow_suppressions: true
211228
- build: Debug
212229
asset: debug-debug-no-copy-on-write
230+
cow_suppressions: false
213231
steps:
214232
- name: Checkout
215233
uses: actions/checkout@v2
@@ -235,48 +253,14 @@ jobs:
235253
run: |
236254
sudo mkdir -p /var/lib/scache/
237255
sudo chmod 0777 /var/lib/scache/
238-
chmod +x ./src/server/scache
239-
240-
241-
pids=""
242-
function wait_all {
243-
for p in $pids; do
244-
IFS=" " read -r -a arrIN <<< "${p//:/ }"
245-
if wait "${arrIN[0]}"; then
246-
echo "Task \"${arrIN[1]}\" (${arrIN[0]}) succeeded"
247-
else
248-
echo "Task \"${arrIN[1]}\" (${arrIN[0]}) fail"
249-
exit 1
250-
fi
251-
done
252-
}
253-
254-
function run_tests {
255-
set -x
256-
set +e
257-
for i in {1..30}; do
258-
curl 127.0.0.1:8081 --max-time 1 --connect-timeout 1
259-
if [[ $? == 0 ]]; then
260-
echo "ready to start php test"
261-
break
262-
fi
263-
sleep 1
264-
done
265-
266-
set -e
267-
set -x
268-
for x in tests/php/test_*.php; do
269-
echo "Doing test $x"
270-
php "$x"
271-
done
272-
}
273-
274-
run_tests &
275-
pids="$!:php"
276-
277-
timeout --kill-after=5 --preserve-status 2m valgrind --leak-check=full --show-leak-kinds=all src/server/scache -b 127.0.0.1:8081 -B 127.0.0.1:8082
278-
279-
wait_all
256+
chmod +x ./src/server/scache ./run-php-tests.sh
257+
PWD=$(pwd)
258+
VALGRIND_SUPPRESSIONS=""
259+
if [[ "${{ matrix.target.cow_suppressions }}" == "true" ]]; then
260+
VALGRIND_SUPPRESSIONS=" --suppressions=$PWD/valgrind-cow-child.supp"
261+
fi
262+
export SCACHE_VALGRIND=1
263+
export SCACHE_VALGRIND_ARGS="--leak-check=full --show-leak-kinds=all$VALGRIND_SUPPRESSIONS"
264+
./run-php-tests.sh
280265
shell: bash
281266

282-

run-php-tests.sh

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ PORT="${2:-$RANDOM_PORT}"
1515
HOST="127.0.0.1"
1616
PIDFILE="/tmp/scache-consistency-test.pid"
1717
DBDIR="/tmp/scache-consistency-test-db"
18-
SCACHE_BIN="$SCRIPT_DIR/src/server/scache"
18+
SCACHE_BIN_REAL="$SCRIPT_DIR/src/server/scache"
19+
SCACHE_BIN="$SCACHE_BIN_REAL"
20+
VALGRIND_WRAPPER="/tmp/scache-consistency-valgrind.sh"
1921

2022
# Colors
2123
RED='\033[0;31m'
@@ -35,28 +37,51 @@ echo "Tests: $TEST_SELECTOR"
3537
echo ""
3638

3739
# Build scache if needed
38-
if [ ! -x "$SCACHE_BIN" ]; then
40+
if [ ! -x "$SCACHE_BIN_REAL" ]; then
3941
echo -e "${YELLOW}Building simple-cache...${NC}"
4042
cd "$SCRIPT_DIR"
4143
make clean && make
42-
if [ ! -x "$SCACHE_BIN" ]; then
44+
if [ ! -x "$SCACHE_BIN_REAL" ]; then
4345
echo -e "${RED}Failed to build simple-cache${NC}"
4446
exit 1
4547
fi
4648
fi
4749

50+
if [ "${SCACHE_VALGRIND:-0}" = "1" ]; then
51+
cat > "$VALGRIND_WRAPPER" <<EOF
52+
#!/bin/bash
53+
exec valgrind ${SCACHE_VALGRIND_ARGS:---leak-check=full --show-leak-kinds=all} "$SCACHE_BIN_REAL" "\$@"
54+
EOF
55+
chmod +x "$VALGRIND_WRAPPER"
56+
SCACHE_BIN="$VALGRIND_WRAPPER"
57+
fi
58+
59+
export SCACHE_PIDFILE="$PIDFILE"
60+
export SCACHE_DBDIR="$DBDIR"
61+
export SCACHE_BIN
62+
4863
# Clean up any previous run
4964
cleanup() {
5065
if [ -f "$PIDFILE" ]; then
5166
PID=$(cat "$PIDFILE" 2>/dev/null)
5267
if [ -n "$PID" ] && [ "$PID" != "0" ] && [ "$PID" != "" ] && kill -0 "$PID" 2>/dev/null; then
53-
kill -9 "$PID" 2>/dev/null || true
68+
kill -TERM "$PID" 2>/dev/null || true
69+
for _ in $(seq 1 50); do
70+
if ! kill -0 "$PID" 2>/dev/null; then
71+
break
72+
fi
73+
sleep 0.1
74+
done
75+
if kill -0 "$PID" 2>/dev/null; then
76+
kill -KILL "$PID" 2>/dev/null || true
77+
fi
5478
fi
5579
rm -f "$PIDFILE"
5680
fi
5781
rm -rf "$DBDIR"
5882
}
5983
cleanup
84+
trap 'rm -f "$VALGRIND_WRAPPER"' EXIT
6085

6186
# Start server
6287
start_server() {

src/core/connection.cpp

Lines changed: 106 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ struct connections_queued
6969
static volatile connections_queued* cq_head = NULL;
7070
static volatile connections_queued* cq_tail = NULL;
7171
static pthread_mutex_t cq_lock;
72+
static bool cq_lock_initialized = false;
7273

7374
/* Methods */
7475
static bool connection_event_update(scache_connection* conn, uint32_t events) {
@@ -104,6 +105,10 @@ void connection_setup(struct scache_binds cache_binds, struct scache_binds monit
104105
if(scache_listeners.listeners == NULL){
105106
FATAL("Unable to allocate memory for listeners");
106107
}
108+
memset(scache_listeners.listeners, 0, sizeof(struct listener_entry) * scache_listeners.listener_count);
109+
for (uint32_t j = 0; j < scache_listeners.listener_count; j++) {
110+
scache_listeners.listeners[j].fd = -1;
111+
}
107112

108113
// Caching
109114
for (i = 0; i < cache_binds.num; i++)
@@ -156,12 +161,15 @@ void connection_close_listeners() {
156161
for (uint32_t i = 0; i < scache_listeners.listener_count; i++)
157162
{
158163
fd = scache_listeners.listeners[i].fd;
159-
scache_listeners.listeners[i].fd = -1;
160-
close(fd);
164+
scache_listeners.listeners[i].fd = -1;
165+
if (fd >= 0) {
166+
close(fd);
167+
}
161168
}
162169

163170
free(scache_listeners.listeners);
164171
scache_listeners.listeners = NULL;
172+
scache_listeners.listener_count = 0;
165173
}
166174

167175
static int connection_open_bind(struct scache_bind ibind, int listenfd)
@@ -494,6 +502,7 @@ void connection_event_loop(void (*connection_handler)(scache_connection* connect
494502
{
495503
PFATAL("mutex init failed");
496504
}
505+
cq_lock_initialized = true;
497506

498507
// Prepare a non blocking eventfd for thread communication
499508
efd = eventfd(0, EFD_NONBLOCK);
@@ -739,9 +748,13 @@ void connection_cleanup() {
739748
connection_close_listeners();
740749
}
741750

742-
// free active connections
743-
for (auto it = connections.begin(); it != connections.end(); ++it) {
744-
connection_cleanup_http(*it);
751+
// Free active connections while erasing set nodes as we go so the set
752+
// itself releases all allocator-owned memory during shutdown.
753+
while (!connections.empty()) {
754+
auto it = connections.begin();
755+
scache_connection* connection = *it;
756+
connections.erase(it);
757+
connection_cleanup_http(connection);
745758
}
746759

747760
// free queued connections
@@ -750,4 +763,91 @@ void connection_cleanup() {
750763
cq_head = cq_head->next;
751764
free(temp);
752765
}
753-
}
766+
cq_tail = NULL;
767+
if (cq_lock_initialized) {
768+
pthread_mutex_destroy(&cq_lock);
769+
cq_lock_initialized = false;
770+
}
771+
}
772+
773+
void connection_release_inherited_fds_after_fork() {
774+
if (scache_listeners.listeners != NULL) {
775+
for (uint32_t i = 0; i < scache_listeners.listener_count; i++) {
776+
int fd = scache_listeners.listeners[i].fd;
777+
scache_listeners.listeners[i].fd = -1;
778+
if (fd >= 0) {
779+
close(fd);
780+
}
781+
}
782+
}
783+
784+
for (auto connection : connections) {
785+
if (connection->client_sock >= 0) {
786+
close(connection->client_sock);
787+
connection->client_sock = -1;
788+
}
789+
}
790+
791+
for (connections_queued* queued = (connections_queued*)cq_head; queued != NULL; queued = queued->next) {
792+
if (queued->client_sock >= 0) {
793+
close(queued->client_sock);
794+
queued->client_sock = -1;
795+
}
796+
}
797+
798+
if (epfd > 0) {
799+
close(epfd);
800+
epfd = -1;
801+
}
802+
}
803+
804+
void connection_cleanup_after_fork() {
805+
DEBUG("Performing post-fork cleanup\n");
806+
807+
connections_queued* temp;
808+
if (scache_listeners.listeners != NULL) {
809+
for (uint32_t i = 0; i < scache_listeners.listener_count; i++) {
810+
int fd = scache_listeners.listeners[i].fd;
811+
scache_listeners.listeners[i].fd = -1;
812+
if (fd >= 0) {
813+
close(fd);
814+
}
815+
}
816+
free(scache_listeners.listeners);
817+
scache_listeners.listeners = NULL;
818+
scache_listeners.listener_count = 0;
819+
}
820+
821+
while (!connections.empty()) {
822+
auto it = connections.begin();
823+
scache_connection* connection = *it;
824+
connections.erase(it);
825+
826+
http_cleanup(connection);
827+
if (connection->client_sock >= 0) {
828+
close(connection->client_sock);
829+
connection->client_sock = -1;
830+
}
831+
free(connection);
832+
}
833+
834+
while (cq_head != NULL) {
835+
temp = (connections_queued*)cq_head;
836+
cq_head = cq_head->next;
837+
if (temp->client_sock >= 0) {
838+
close(temp->client_sock);
839+
}
840+
free(temp);
841+
}
842+
cq_tail = NULL;
843+
844+
if (epfd > 0) {
845+
close(epfd);
846+
epfd = -1;
847+
}
848+
849+
if (cq_lock_initialized) {
850+
pthread_mutex_destroy(&cq_lock);
851+
cq_lock_initialized = false;
852+
}
853+
}

src/core/connection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ void connection_close_listeners();
1111
void connection_event_loop(void(*connection_handler)(scache_connection* connection), int monitoring_fd);
1212
void connection_setup(struct scache_binds cache_binds, struct scache_binds cache_monitor);
1313
void connection_cleanup();
14+
void connection_release_inherited_fds_after_fork();
15+
void connection_cleanup_after_fork();
1416
bool connection_remove(scache_connection* conn);
1517
bool connection_stop_soon();
1618

0 commit comments

Comments
 (0)