Two things were wrong with .splinter-swarm.sock. Every chat bound it, wanted or not, so a shared working directory collected a leftover from every session and the next start that found one sat on "[swarm locked]" because nobody answered on it. And the master removed the file on its way out even when a member was still running, which left that member on a path that was gone. Now the fleet is opt-in. Without --swarm nothing binds or joins the file; with it, the file lives exactly as long as the fleet does, whatever order the members leave in. - main.c: --swarm is a flag; eo.swarm requires it (plus --chat, no --serve, no --daemon, no A2A/MCP/delegate). - swarm.c: join_or_bind() is the one place that decides master or slave, used at startup and again on takeover. A slave whose master says "bye" or whose connection just ends (SIGKILL) takes the fleet over: bind, or join whoever was faster. A leftover file with no listener is removed there and rebound, because a refused connect is proof nobody answers. - master_close() removes the file only when it is the last member. The peer check reads the sockets instead of trusting the cached flag: a peer that exited is only visible as EOF on read. - swarm_emergency_unlink() (SIGTERM/SIGHUP) keeps the file while a peer is live. A handler may not walk the peer table, so the master keeps a sig_atomic_t count of live peers for it. Tests, all against the real binary and the real signal path: - test_swarm.c: master leaves first, survivor takes over and the file stays; hard-killed master (EOF, no bye), survivor cleans the leftover up; the signal cleanup alone leaves the file for a live member; an unowned file is still never deleted. Mutation-checked: forcing master_close to always unlink, and dropping the peer count from swarm_emergency_unlink, each turn the suite red. - test_swarm_socket.sh (new, in make check): through tmux, --chat without --swarm creates no file at all; --chat --swarm creates one that accepts a member and is gone after Ctrl-D and after SIGTERM; two members share one file that outlives the master and goes with the last one; a SIGTERM-killed master does not take the fleet from the survivor. Mutation-checked: making the fleet unconditional (the old eo.swarm line) turns it red. make check: all tests passed. check_asan.sh: all 46 test binaries clean.
294 lines
7.8 KiB
C
294 lines
7.8 KiB
C
/*
|
|
* splinter -- swarm (local fleet) tests.
|
|
*
|
|
* Two instances in one directory, in one process: the first binds the socket
|
|
* and becomes master, the second connects as a slave. What is asserted is the
|
|
* wire cycle -- welcome, id, routed text, busy report -- not the UI.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "swarm.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static void
|
|
nap_ms(long ms)
|
|
{
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = ms / 1000;
|
|
ts.tv_nsec = (ms % 1000) * 1000000L;
|
|
nanosleep(&ts, NULL);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct swarm master, slave;
|
|
char dir[] = "/tmp/splinter-swarm-XXXXXX";
|
|
char buf[512], list[512], path[600];
|
|
int i, got = 0;
|
|
|
|
CHECK(mkdtemp(dir) != NULL);
|
|
|
|
CHECK(swarm_init(&master, dir) == 0);
|
|
CHECK(master.role == SWARM_MASTER);
|
|
CHECK(master.id == 0);
|
|
|
|
CHECK(swarm_init(&slave, dir) == 0);
|
|
CHECK(slave.role == SWARM_SLAVE);
|
|
|
|
/* the master has to accept before the welcome reaches the slave */
|
|
for (i = 0; i < 50 && slave.id < 0; i++) {
|
|
swarm_poll(&master);
|
|
swarm_poll(&slave);
|
|
if (slave.id < 0)
|
|
nap_ms(10);
|
|
}
|
|
CHECK(slave.id == 1);
|
|
|
|
/* a routed line arrives in the slave's inbox and says it came from afar */
|
|
CHECK(swarm_route(&master, -1, "hallo slave") == 1);
|
|
for (i = 0; i < 50 && !got; i++) {
|
|
swarm_poll(&slave);
|
|
got = swarm_take_inbox(&slave, buf, sizeof(buf));
|
|
if (!got)
|
|
nap_ms(10);
|
|
}
|
|
CHECK(got == 1);
|
|
CHECK(strcmp(buf, "hallo slave") == 0);
|
|
CHECK(swarm_inbox_remote(&slave) == 1);
|
|
CHECK(swarm_inbox_remote(&slave) == 0); /* cleared after reading */
|
|
|
|
/* quotes, backslashes and newlines survive the wire unescaped */
|
|
got = 0;
|
|
CHECK(swarm_route(&master, 1, "say \"hi\" \\ now\nline 2") == 1);
|
|
for (i = 0; i < 50 && !got; i++) {
|
|
swarm_poll(&slave);
|
|
got = swarm_take_inbox(&slave, buf, sizeof(buf));
|
|
if (!got)
|
|
nap_ms(10);
|
|
}
|
|
CHECK(got == 1);
|
|
CHECK(strcmp(buf, "say \"hi\" \\ now\nline 2") == 0);
|
|
(void)swarm_inbox_remote(&slave);
|
|
|
|
/* the master can route to itself without a socket round trip */
|
|
CHECK(swarm_route(&master, 0, "an mich") == 0);
|
|
CHECK(swarm_take_inbox(&master, buf, sizeof(buf)) == 1);
|
|
CHECK(strcmp(buf, "an mich") == 0);
|
|
CHECK(swarm_inbox_remote(&master) == 1);
|
|
|
|
/* a busy report is visible in the master's list */
|
|
swarm_report_busy(&slave, 7);
|
|
for (i = 0; i < 50; i++) {
|
|
swarm_poll(&master);
|
|
swarm_list(&master, list, sizeof(list));
|
|
if (strstr(list, "busy=7") != NULL)
|
|
break;
|
|
nap_ms(10);
|
|
}
|
|
CHECK(strstr(list, "busy=7") != NULL);
|
|
CHECK(strstr(list, "slave pid=") != NULL);
|
|
|
|
swarm_status(&master, buf, sizeof(buf));
|
|
CHECK(strstr(buf, "master 0") != NULL);
|
|
CHECK(strstr(buf, "1 slaves") != NULL);
|
|
swarm_status(&slave, buf, sizeof(buf));
|
|
CHECK(strstr(buf, "slave 1") != NULL);
|
|
|
|
/*
|
|
* The file lives while a member does, and not a second longer. Who bound
|
|
* it is an implementation detail: when the master goes, a member that is
|
|
* still there takes the fleet over and the path stays.
|
|
*/
|
|
snprintf(path, sizeof(path), "%s/%s", dir, SWARM_PATH);
|
|
CHECK(access(path, F_OK) == 0);
|
|
/* close the slave first: it is not the one that would keep the fleet */
|
|
swarm_close(&slave);
|
|
CHECK(access(path, F_OK) == 0); /* the master is still there */
|
|
swarm_close(&master);
|
|
if (access(path, F_OK) == 0) {
|
|
fprintf(stderr, "FAIL %s:%d: socket file survived the last member\n",
|
|
__FILE__, __LINE__);
|
|
failures++;
|
|
}
|
|
CHECK(access(path, F_OK) != 0);
|
|
|
|
/*
|
|
* Two members, the master leaves first: the fleet stays alive under the
|
|
* remaining one and the file with it. The old close took the file along
|
|
* and left the survivor on a path nobody answered on.
|
|
*/
|
|
{
|
|
struct swarm m2, s2;
|
|
|
|
CHECK(swarm_init(&m2, dir) == 0);
|
|
CHECK(m2.role == SWARM_MASTER);
|
|
CHECK(swarm_init(&s2, dir) == 0);
|
|
CHECK(s2.role == SWARM_SLAVE);
|
|
for (i = 0; i < 50 && s2.id < 0; i++) {
|
|
swarm_poll(&m2);
|
|
swarm_poll(&s2);
|
|
if (s2.id < 0)
|
|
nap_ms(10);
|
|
}
|
|
CHECK(s2.id == 1);
|
|
|
|
swarm_close(&m2); /* the master goes first */
|
|
CHECK(access(path, F_OK) == 0); /* a member is still running */
|
|
|
|
/* the survivor notices and takes the fleet over */
|
|
for (i = 0; i < 50 && s2.role != SWARM_MASTER; i++) {
|
|
swarm_poll(&s2);
|
|
if (s2.role != SWARM_MASTER)
|
|
nap_ms(10);
|
|
}
|
|
CHECK(s2.role == SWARM_MASTER);
|
|
CHECK(access(path, F_OK) == 0);
|
|
|
|
/* and it is the last one, so its close removes the file */
|
|
swarm_close(&s2);
|
|
CHECK(access(path, F_OK) != 0);
|
|
}
|
|
|
|
/*
|
|
* A hard-killed master (EOF on the connection, no "bye") leaves a file
|
|
* with no listener behind. A member that reads the EOF has to clean that
|
|
* up and keep the fleet -- this is the "[swarm locked]" case.
|
|
*/
|
|
{
|
|
struct swarm m3, s3;
|
|
|
|
CHECK(swarm_init(&m3, dir) == 0);
|
|
CHECK(swarm_init(&s3, dir) == 0);
|
|
for (i = 0; i < 50 && s3.id < 0; i++) {
|
|
swarm_poll(&m3);
|
|
swarm_poll(&s3);
|
|
if (s3.id < 0)
|
|
nap_ms(10);
|
|
}
|
|
CHECK(s3.id == 1);
|
|
/* the master dies the way kill -9 does: no bye, file left over */
|
|
if (m3.listen_fd >= 0) {
|
|
close(m3.listen_fd);
|
|
m3.listen_fd = -1;
|
|
}
|
|
for (i = 0; i < m3.npeers; i++)
|
|
if (m3.peers[i].fd >= 0) {
|
|
close(m3.peers[i].fd);
|
|
m3.peers[i].fd = -1;
|
|
}
|
|
m3.role = SWARM_NONE;
|
|
CHECK(access(path, F_OK) == 0); /* the leftover file */
|
|
|
|
for (i = 0; i < 50 && s3.role != SWARM_MASTER; i++) {
|
|
swarm_poll(&s3);
|
|
if (s3.role != SWARM_MASTER)
|
|
nap_ms(10);
|
|
}
|
|
CHECK(s3.role == SWARM_MASTER);
|
|
swarm_close(&s3);
|
|
CHECK(access(path, F_OK) != 0);
|
|
}
|
|
|
|
/*
|
|
* The last-resort signal cleanup must not take the fleet away from a
|
|
* member that is still running. kill -TERM on the master runs
|
|
* swarm_emergency_unlink(), which has no access to the peer table and
|
|
* counts live peers in a sig_atomic_t instead: with a peer there, the
|
|
* file stays and the survivor takes over.
|
|
*/
|
|
{
|
|
struct swarm m4, s4;
|
|
|
|
CHECK(swarm_init(&m4, dir) == 0);
|
|
CHECK(swarm_init(&s4, dir) == 0);
|
|
for (i = 0; i < 50 && s4.id < 0; i++) {
|
|
swarm_poll(&m4);
|
|
swarm_poll(&s4);
|
|
if (s4.id < 0)
|
|
nap_ms(10);
|
|
}
|
|
CHECK(s4.id == 1);
|
|
/* what the SIGTERM handler would do to the master */
|
|
swarm_emergency_unlink();
|
|
if (access(path, F_OK) != 0) {
|
|
fprintf(stderr,
|
|
"FAIL %s:%d: signal cleanup took the file from a live member\n",
|
|
__FILE__, __LINE__);
|
|
failures++;
|
|
}
|
|
/* and then the process is gone: no bye, no close, just EOF */
|
|
if (m4.listen_fd >= 0) {
|
|
close(m4.listen_fd);
|
|
m4.listen_fd = -1;
|
|
}
|
|
for (i = 0; i < m4.npeers; i++)
|
|
if (m4.peers[i].fd >= 0) {
|
|
close(m4.peers[i].fd);
|
|
m4.peers[i].fd = -1;
|
|
}
|
|
m4.role = SWARM_NONE;
|
|
/* the survivor has to take over the fleet that was left to it */
|
|
for (i = 0; i < 50 && s4.role != SWARM_MASTER; i++) {
|
|
swarm_poll(&s4);
|
|
if (s4.role != SWARM_MASTER)
|
|
nap_ms(10);
|
|
}
|
|
CHECK(s4.role == SWARM_MASTER);
|
|
swarm_close(&s4);
|
|
CHECK(access(path, F_OK) != 0);
|
|
}
|
|
|
|
/* a directory with no fleet is standalone, not an error */
|
|
{
|
|
struct swarm solo;
|
|
|
|
CHECK(swarm_init(&solo, dir) == 0);
|
|
CHECK(solo.role == SWARM_MASTER);
|
|
CHECK(access(path, F_OK) == 0);
|
|
swarm_close(&solo);
|
|
CHECK(access(path, F_OK) != 0);
|
|
}
|
|
|
|
/*
|
|
* A file that is not ours is not deleted. Another instance may have
|
|
* bound the path after us; its file must survive our close.
|
|
*/
|
|
{
|
|
struct swarm first;
|
|
FILE *foreign;
|
|
|
|
CHECK(swarm_init(&first, dir) == 0);
|
|
CHECK(first.role == SWARM_MASTER);
|
|
/* replace the file behind our back, as a new master would */
|
|
CHECK(unlink(path) == 0);
|
|
foreign = fopen(path, "w");
|
|
CHECK(foreign != NULL);
|
|
if (foreign != NULL)
|
|
fclose(foreign);
|
|
swarm_close(&first);
|
|
CHECK(access(path, F_OK) == 0); /* not ours: left alone */
|
|
(void)unlink(path);
|
|
}
|
|
|
|
snprintf(path, sizeof(path), "rm -rf '%s'", dir);
|
|
(void)system(path);
|
|
|
|
if (failures == 0)
|
|
printf("test_swarm: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|