Http-Server in C, Stage #EJ5, failed to read response (no content received)

I’m stuck on Stage #EJ5 (Concurrent connections)

For implemeting concurrency I use pthreads.h , but in doing so tests wont pass. I set up a macro CONCURRENT in order to be able to test against previous stages. When CONCURRENT == 0 , all tests go ok but this last one. For using threads I toggle it to 1, first tests fail. However in my machine works flawlessly without errors both ways, so not sure how to debug it.

Here are my logs:

[tester::#EJ5] Running tests for Stage #EJ5 (Concurrent connections)
[tester::#EJ5] Running program
[tester::#EJ5] $ ./your_program.sh
[tester::#EJ5] Creating 2 parallel connections
[tester::#EJ5] Creating connection 1
[your_program] Listening to port 4221...
[tester::#EJ5] Creating connection 2
[your_program] handling client...
[your_program] thread id: -1832678624
[tester::#EJ5] Sending first set of requests
[tester::#EJ5] client-2: $ curl -v http://localhost:4221/
[tester::#EJ5] client-2: > GET / HTTP/1.1
[tester::#EJ5] client-2: > Host: localhost:4221
[tester::#EJ5] client-2: >
[tester::#EJ5] client-2: Sent bytes: "GET / HTTP/1.1\r\nHost: localhost:4221\r\n\r\n"
[your_program] thread id: -1832821984
[your_program] handling client...
[tester::#EJ5] Failed to read response:
[tester::#EJ5] Received: "" (no content received)
[tester::#EJ5]            ^ error
[tester::#EJ5] Error: Expected: HTTP-version, Received: ""
[tester::#EJ5] Test failed
[tester::#EJ5] Terminating program
[tester::#EJ5] Program terminated successfully

And here’s a snippet of my code, part of main inside the while loop and the handle_client function:

#if CONCURRENT == 1
        pthread_t thread_id;
        int s = pthread_create(&thread_id, NULL, handle_client, client_sock_ptr);
        if (s != 0) {
          printf("error of pthread_create id: %d\n", s);
        }
        pthread_detach(thread_id);
        printf("thread id: %d\n", thread_id);
#else
        printf("not threading...\n");
        handle_client(client_sock_ptr);
#endif

void *handle_client(void *client_socket)
{
    printf("handling client...\n");
    int sock = *(int *)client_socket;
    free(client_socket);

    char buffer[BUFFER_SIZE] = {0};
    int bytes_read = read(sock, buffer, BUFFER_SIZE - 1);

    HttpRequest req = {0};
    parse_http_request(buffer, &req);

    char response[4096] = {0};
    // CODECRAFTERS TEST DON'T GET TO THE NEXT LINE?
    handle_request(&req, response);

    if (DEBUG == 1) {
        printf("\nbytes read: %d\n", bytes_read);
        print_http_request(&req);
        printf("\nresponse:\n<<<<<<<\n%s\n>>>>>>>\n", response);
    }

    send(sock, response, strlen(response), 0);
    close(sock);

    return NULL;
}

I’ll take a look and get back to you by Monday.

1 Like

Hi @amarjen, the code can pass most stages if these lines are commented out:

I’m not entirely sure why httpHeaders causes your code to hang. I’ll check with the team and get back to you with more details.

Will keep this thread open until we upgrade the C build pack.