Professional Web Applications Themes

Problem closing a socket while thread blocks in read() - UNIX Programming

I have a child thread that's blocking in a read() on a TCP socket. At the same time, my main thread closes the socket via close(). When this happens, read() never returns in the child thread and the socket is not reported as closed to the other end until the program exits. Here's a program that demonstrates the problem: #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <netinet/in.h> #include <netdb.h> #include <sys/time.h> void* read_thread(void* data) { int s = *(int*)data; char buff[256]; puts("Reading in child thread"); int result = read(s, buff, sizeof buff); printf("Read returned %d\n", result); } int ...

  1. #1

    Default Problem closing a socket while thread blocks in read()

    I have a child thread that's blocking in a read() on a TCP socket. At
    the same time, my main thread closes the socket via close(). When this
    happens, read() never returns in the child thread and the socket is not
    reported as closed to the other end until the program exits.

    Here's a program that demonstrates the problem:

    #include <sys/types.h>
    #include <sys/socket.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <sys/time.h>

    void* read_thread(void* data) {
    int s = *(int*)data;
    char buff[256];
    puts("Reading in child thread");
    int result = read(s, buff, sizeof buff);
    printf("Read returned %d\n", result);
    }

    int main(void) {
    int s = socket(AF_INET, SOCK_STREAM, 0);
    if (s < 2) {
    printf("socket returned %d\n", s);
    return EXIT_FAILURE;
    }
    struct sockaddr_in address;
    memset(&address, 0, sizeof address);
    address.sin_family = AF_INET;
    address.sin_port = htons(1109);
    struct hostent* hp = gethostbyname("localhost");
    memcpy((char *)&address.sin_addr,hp->h_addr,hp->h_length);
    if (connect(s, (struct sockaddr*)&address, sizeof address) < 0) {
    printf("connect failed\n");
    return EXIT_FAILURE;
    }
    puts("Connection succeeded");

    puts("Spawning thread");
    pthread_t thread;
    pthread_create(&thread, NULL, read_thread, &s);

    //block this thread for 5 seconds via select
    struct timeval timeout={5, 0};
    select(0, NULL, NULL, NULL, &timeout);

    puts("Closing");
    if (close(s) < 0) {
    puts("Close failed");
    return EXIT_FAILURE;
    }
    puts("Looping in main thread");
    for (;;);
    return 0;
    }

    That code never reports read() as returning. Can anyone shed light on
    this situation? My system is Mac OS X 10.3.2.

    Thanks.

    Peter Guest

  2. #2

    Default Re: Problem closing a socket while thread blocks in read()


    "Peter Ammon" <com> wrote in message
    news:c20uko$1ij$apple.com...

     


    I'm afraid that you can never guarantee what will happen when you do
    this. This has been discussed to death, but the basic idea is that you can
    never be sure the child thread is actually blocking in the 'read' as opposed
    to about to block in the 'read'.

    DS




    David Guest

  3. #3

    Default Re: Problem closing a socket while thread blocks in read()



    Peter Ammon wrote: 

    Why should you close the socket in the main thread?
    Lorinczy Guest

  4. #4

    Default Re: Problem closing a socket while thread blocks in read()

    Peter Ammon <com> writes:
     

    I suspect that if the socket is already involved in a blocking read,
    the close won't get processed until the read returns something.

    Why would you do that though? If you want to timeout the read, why not
    put the select() in the reading thread?

    Joe
    Joe Guest

Similar Threads

  1. Replies: 3
    Last Post: November 12th, 02:17 PM
  2. Replies: 1
    Last Post: September 11th, 01:41 PM
  3. AMD Thread info - LenHewitt, please read
    By Cheesefood in forum Adobe Photoshop 7, CS, CS2 & CS3
    Replies: 2
    Last Post: August 13th, 05:18 PM
  4. About undo blocks & read consistency
    By Spendius in forum Oracle Server
    Replies: 4
    Last Post: July 9th, 10:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139