Skip to content

Commit 04d02f5

Browse files
mse-pnclaude
andcommitted
🐛 fix: revert to stock dtp_server_main (custom loop caused len=0)
The custom server loop received packets with len=0 while the stock dtp_server_run received them correctly. Revert to using dtp_server_main with a 200ms startup delay to ensure port 7 is bound before the deploy request is sent. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a761361 commit 04d02f5

1 file changed

Lines changed: 9 additions & 78 deletions

File tree

satdeploy-apm/src/satdeploy_apm.c

Lines changed: 9 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <apm/csh_api.h>
2727
#include <dtp/dtp.h>
2828
#include <dtp/dtp_file_payload.h>
29-
#include <dtp/dtp_protocol.h>
3029
#include "deploy.pb-c.h"
3130
#include "config.h"
3231
#include "history.h"
@@ -289,79 +288,16 @@ bool get_payload_meta(dtp_payload_meta_t *meta, uint8_t payload_id) {
289288
return result;
290289
}
291290

292-
/* DTP server thread — custom loop that holds the RDP connection open
293-
* long enough for the client to read the metadata response.
294-
* The stock dtp_server_run() closes immediately after sending data,
295-
* which races with the client on small files. */
291+
/* DTP server thread context */
296292
typedef struct {
297293
volatile bool exit_flag;
298-
volatile bool ready; /* set once port 7 is bound */
299-
int bind_error;
294+
volatile bool ready;
300295
} dtp_server_ctx_t;
301296

302-
/* Static socket — must outlive the thread since the CSP port table
303-
* holds a pointer to it. Stack-local would leave a dangling ref. */
304-
static csp_socket_t dtp_server_sock;
305-
306297
static void *dtp_server_thread(void *arg) {
307298
dtp_server_ctx_t *ctx = (dtp_server_ctx_t *)arg;
308-
309-
memset(&dtp_server_sock, 0, sizeof(dtp_server_sock));
310-
dtp_server_sock.opts = CSP_O_RDP;
311-
312-
int rc = csp_bind(&dtp_server_sock, 7);
313-
if (rc != CSP_ERR_NONE) {
314-
printf("[dtp-server] Failed to bind port 7 (error %d)\n", rc);
315-
fflush(stdout);
316-
ctx->bind_error = rc;
317-
ctx->ready = true; /* unblock main thread */
318-
return NULL;
319-
}
320-
csp_listen(&dtp_server_sock, 1);
321-
printf("[dtp-server] Listening on port 7\n");
322-
fflush(stdout);
323-
ctx->ready = true; /* signal main thread: ready to accept */
324-
325-
while (!ctx->exit_flag) {
326-
csp_conn_t *conn = csp_accept(&dtp_server_sock, 1000);
327-
if (!conn) continue;
328-
329-
csp_packet_t *packet = csp_read(conn, 10000);
330-
if (!packet) {
331-
csp_close(conn);
332-
continue;
333-
}
334-
335-
printf("[dtp-server] Got metadata request from node %d, len=%u\n",
336-
csp_conn_src(conn), packet->length);
337-
printf("[dtp-server] raw packet data (first 16 bytes):");
338-
for (int i = 0; i < 16 && i < packet->length; i++)
339-
printf(" %02x", packet->data[i]);
340-
printf("\n");
341-
fflush(stdout);
342-
343-
packet = setup_server_transfer(&server_transfer_ctx, csp_conn_src(conn), packet);
344-
server_transfer_ctx.keep_running = true;
345-
if (packet) {
346-
csp_send(conn, packet);
347-
/* Give the client time to process the metadata response
348-
* before we start blasting data packets */
349-
usleep(200000); /* 200ms */
350-
start_sending_data(&server_transfer_ctx);
351-
printf("[dtp-server] Data sent, holding connection...\n");
352-
fflush(stdout);
353-
}
354-
server_transfer_ctx.keep_running = false;
355-
356-
/* Hold the RDP connection open so the RST doesn't race
357-
* with the metadata response on small transfers */
358-
usleep(500000); /* 500ms */
359-
csp_close(conn);
360-
printf("[dtp-server] Transfer complete\n");
361-
fflush(stdout);
362-
}
363-
364-
csp_socket_close(&dtp_server_sock);
299+
ctx->ready = true;
300+
dtp_server_main(&ctx->exit_flag);
365301
return NULL;
366302
}
367303

@@ -480,25 +416,20 @@ static int deploy_single_app(unsigned int node, char *app_name,
480416
fflush(stdout);
481417

482418
/* Step 2: Start DTP server in background thread */
483-
dtp_server_ctx_t dtp_ctx = { .exit_flag = false, .ready = false, .bind_error = 0 };
419+
dtp_server_ctx_t dtp_ctx = { .exit_flag = false, .ready = false };
484420
pthread_t dtp_thread;
485421
if (pthread_create(&dtp_thread, NULL, dtp_server_thread, &dtp_ctx) != 0) {
486422
printf("Error: Failed to start DTP server thread\n");
487423
dtp_file_payload_del(payload_id);
488424
return SLASH_EIO;
489425
}
490426

491-
/* Wait for server to bind port 7 before sending deploy request */
427+
/* Wait for server thread to start */
492428
for (int i = 0; i < 50 && !dtp_ctx.ready; i++) {
493-
usleep(20000); /* 20ms, max 1s total */
494-
}
495-
if (dtp_ctx.bind_error || !dtp_ctx.ready) {
496-
printf("Error: DTP server failed to start (bind_error=%d)\n", dtp_ctx.bind_error);
497-
dtp_ctx.exit_flag = true;
498-
pthread_join(dtp_thread, NULL);
499-
dtp_file_payload_del(payload_id);
500-
return SLASH_EIO;
429+
usleep(20000);
501430
}
431+
/* Give dtp_server_main time to bind port 7 */
432+
usleep(200000);
502433

503434
/* Step 3: Send CMD_DEPLOY — agent will pull the file via DTP */
504435
csp_iface_t *default_iface = csp_iflist_get_by_isdfl(NULL);

0 commit comments

Comments
 (0)