Skip to content

using the signal to exit the program may cause memory leak #3416

@yjqhh

Description

@yjqhh

Hello, Andy. Recently, I encountered the memory leak issue in 4.3.1. When in a state of no network, I use ctrl + c exit the program quickly, the issue has occurred.Then I skipped the steps of lws_service and can easily reproduce it. The issue will also be reproduced using ctrl + c during DNS retry process.

static volatile sig_atomic_t exit_flag = 0;

void handle_sigint(int signal) {
    if (signal == SIGINT) {
        exit_flag = 1;
    }
}
 
static int callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len);

static struct lws_protocols protocols[] = {
    {"echo", callback_echo, 0, 0},
    {NULL, NULL, 0, 0 }
};

static int callback_echo(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
    char msg[LWS_PRE + 13];
    char *key = "content-type:";
    char *value = "application/json";
    int length = strlen(value);
    switch (reason) {
    case LWS_CALLBACK_CLIENT_RECEIVE:
        printf("Received: %s\n", (char *)in);
        break;
    case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:

        char **p = (char **)in, *end = (*p) + len;
        if(lws_add_http_header_by_name(wsi, (unsigned char *)key, (unsigned char *)value, length,
        (unsigned char **)p, (unsigned char *)end)) 
            return -1;
        break;
    case LWS_CALLBACK_CLIENT_ESTABLISHED:
        printf("Connection established\n");
        int n = lws_snprintf(msg + LWS_PRE, sizeof(msg) - LWS_PRE,
                      "hello,server!");
        lws_write(wsi, (unsigned char *)msg + LWS_PRE, n, LWS_WRITE_TEXT);
        break;
    case LWS_CALLBACK_CLIENT_CLOSED:
        printf("Connection closed\n");
        break;
    case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
        printf("[Test Protocol] There was a connection error: %s\n", in ? (char*)in : "(no error information)");
        break;
    default:
        printf("default reason %d \n",reason);
        break;
    }
    return 0;
}
 
int main(int argc, char **argv)
{
    struct lws_context_creation_info info;
    memset(&info, 0, sizeof info);
    info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT; 
    info.port = CONTEXT_PORT_NO_LISTEN;
    info.protocols = protocols;
    info.gid = -1;
    info.uid = -1;
    signal(SIGINT, handle_sigint);
    
    struct lws_context *context = lws_create_context(&info);

    struct lws_client_connect_info ccinfo = {0};
    ccinfo.context = context;
    ccinfo.address = "echo.websocket.events";
    ccinfo.port = 80;  
    ccinfo.path = "/"; 
    ccinfo.host = ccinfo.address;
    ccinfo.origin = ccinfo.address;
    ccinfo.protocol = protocols[0].name;
    ccinfo.ssl_connection = 0;
    struct lws *client = lws_client_connect_via_info(&ccinfo);


    // while (!exit_flag) {
    //     lws_service(context, 50);
    // }

    lws_context_destroy(context);
    
    return 0;
}

Image

This is the backtrace of a memory leak

Image

Image
I don't know if this issue has been fixed in the current version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions