Skip to content

Commit

Permalink
Catch hanging commands on server side
Browse files Browse the repository at this point in the history
i.e. a buggy server that never sends a tagged response to a command

Otherwise, imaptest will hang until the connection is terminated (which
might be ~30 minutes)
  • Loading branch information
slusarz authored and cmouse committed Nov 22, 2023
1 parent 6f99cd0 commit 1425ad0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ command_get_cmdline(struct imap_client *client, const char **_cmdline,
*_cmdline_len = str_len(str);
}

static void command_delay_timeout(struct imap_client *client)
{
imap_client_input_error(client,
"Timeout while waiting for server response");
client_disconnect(&client->client);
}

struct command *command_send(struct imap_client *client, const char *cmdline,
command_callback_t *callback)
{
Expand Down Expand Up @@ -203,6 +210,10 @@ command_send_binary(struct imap_client *client, const char *cmdline,
o_stream_nsendv(client->client.output, iov, 3);
i_gettimeofday(&cmd->tv_start);

if (client->delay_timeout_ms > 0)
cmd->delay_to = timeout_add(client->delay_timeout_ms,
command_delay_timeout, client);

array_append(&client->commands, &cmd, 1);
client->last_cmd = cmd;
return cmd;
Expand Down Expand Up @@ -231,6 +242,7 @@ void command_free(struct command *cmd)
{
if (array_is_created(&cmd->seq_range))
array_free(&cmd->seq_range);
timeout_remove(&cmd->delay_to);
i_free(cmd->cmdline);
i_free(cmd);
}
Expand Down
1 change: 1 addition & 0 deletions src/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct command {

command_callback_t *callback;
struct timeval tv_start;
struct timeout *delay_to;

bool expect_bad:1;
};
Expand Down
2 changes: 2 additions & 0 deletions src/imap-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ struct imap_client {

int (*handle_untagged)(struct imap_client *, const struct imap_arg *);

unsigned int delay_timeout_ms;

bool seen_banner:1;
bool append_unfinished:1;
bool try_create_mailbox:1;
Expand Down
3 changes: 3 additions & 0 deletions src/test-exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <ctype.h>

#define IS_VAR_CHAR(c) (i_isalnum(c) || (c) == '_')
#define TEST_EXEC_DELAY_TIMEOUT_SECS 30

struct tests_execute_context {
const ARRAY_TYPE(test) *tests;
Expand Down Expand Up @@ -1320,6 +1321,8 @@ static int test_execute(const struct test *test,
ctx->clients[i]->client.v.send_more_commands =
test_send_lstate_commands;
ctx->clients[i]->test_exec_ctx = ctx;
ctx->clients[i]->delay_timeout_ms =
TEST_EXEC_DELAY_TIMEOUT_SECS * 1000;

key = i == 0 ? "user" : p_strdup_printf(pool, "user%u", i+1);
value = p_strdup(pool, ctx->clients[i]->client.user->username);
Expand Down

0 comments on commit 1425ad0

Please sign in to comment.