read/select not blocking on SSL socket?

Discussion in 'Ruby' started by yermej@gmail.com, Aug 30, 2007.

  1. Guest

    I'm working on an SSL client/server application using persistent TLSv1
    connections. I'm having some issues with my server and what I think
    should be a blocking read or select.

    I'm basically doing this where @io is an OpenSSL::SSL::SSLSocket:

    until @data.length == READ_SIZE do
    tmp = @io.read(READ_SIZE - @data.length)
    if tmp.nil?
    IO.select [@io]
    else
    @data << tmp
    end
    end

    (I did not initially have the select call, but decided to give that a
    try.)

    I would expect that the read would block (I have not set O_NONBLOCK on
    the socket) until there is data to be read. Instead, when there is no
    data, every read call returns nil. I thought the select would fix
    that, but it returns immediately and the read call again returns nil.

    Am I misunderstanding how this should work? Would the fact that it's
    an SSL connection cause the issue?

    I'm using 1.8.4 on Linux.

    Thanks for any insights you can offer.

    Jeremy
    , Aug 30, 2007
    #1
    1. Advertising

  2. Guest

    On Aug 30, 3:20 pm, "" <> wrote:
    > I'm working on an SSL client/server application using persistent TLSv1
    > connections. I'm having some issues with my server and what I think
    > should be a blocking read or select.
    >
    > I'm basically doing this where @io is an OpenSSL::SSL::SSLSocket:
    >
    > until @data.length == READ_SIZE do
    > tmp = @io.read(READ_SIZE - @data.length)
    > if tmp.nil?
    > IO.select [@io]
    > else
    > @data << tmp
    > end
    > end
    >
    > (I did not initially have the select call, but decided to give that a
    > try.)
    >
    > I would expect that the read would block (I have not set O_NONBLOCK on
    > the socket) until there is data to be read. Instead, when there is no
    > data, every read call returns nil. I thought the select would fix
    > that, but it returns immediately and the read call again returns nil.
    >
    > Am I misunderstanding how this should work? Would the fact that it's
    > an SSL connection cause the issue?
    >
    > I'm using 1.8.4 on Linux.
    >
    > Thanks for any insights you can offer.
    >
    > Jeremy


    I have an update to my initial question. I replaced my read calls with
    sysread. With one client who sends to this server, the read or sysread
    blocks as expected. With the other, the read always returns nil and
    sysread always throws EOFError exceptions when I think it should
    block. I catch EOFError and sysread again, but instead of blocking, it
    throws EOFError again. And again. Etc.

    Is this the correct behavior if they are keeping the client end of the
    connection open (the working client -- the one where the read blocks
    -- seems be keeping the connection open) or should the read be
    blocking?

    At this point, I'm beginning to think my only option will be to sleep
    and then check the connection for data in a loop.

    Jeremy
    , Aug 30, 2007
    #2
    1. Advertising

  3. On Aug 30, 1:20 pm, "" <> wrote:
    > I'm working on an SSL client/server application using persistent TLSv1
    > connections. I'm having some issues with my server and what I think
    > should be a blocking read or select.
    >


    I also understood Ruby io.read to be blocking.

    > I'm basically doing this where @io is an OpenSSL::SSL::SSLSocket:
    >
    > until @data.length == READ_SIZE do
    > tmp = @io.read(READ_SIZE - @data.length)
    > if tmp.nil?
    > IO.select [@io]
    > else
    > @data << tmp
    > end
    > end
    >
    > (I did not initially have the select call, but decided to give that a
    > try.)
    >
    > I would expect that the read would block (I have not set O_NONBLOCK on
    > the socket) until there is data to be read. Instead, when there is no
    > data, every read call returns nil. I thought the select would fix
    > that, but it returns immediately and the read call again returns nil.
    >
    > Am I misunderstanding how this should work? Would the fact that it's
    > an SSL connection cause the issue?
    >
    > I'm using 1.8.4 on Linux.
    >
    > Thanks for any insights you can offer.
    >
    > Jeremy



    Maybe I'm not the best person to be giving advice, but I don't think
    your do/end is a legal Ruby construction.

    I was under the impression you would use do/end like the following

    sock.each_line do |line|
    puts line
    end

    Notice the nifty | | thingies. This will read each line and then
    return.


    Chad
    grocery_stocker, Aug 31, 2007
    #3
  4. On Aug 30, 5:58 pm, grocery_stocker <> wrote:
    > On Aug 30, 1:20 pm, "" <> wrote:
    >
    > > I'm working on an SSL client/server application using persistent TLSv1
    > > connections. I'm having some issues with my server and what I think
    > > should be a blocking read or select.

    >
    > I also understood Ruby io.read to be blocking.
    >
    >
    >
    > > I'm basically doing this where @io is an OpenSSL::SSL::SSLSocket:

    >
    > > until @data.length == READ_SIZE do
    > > tmp = @io.read(READ_SIZE - @data.length)
    > > if tmp.nil?
    > > IO.select [@io]
    > > else
    > > @data << tmp
    > > end
    > > end

    >
    > > (I did not initially have the select call, but decided to give that a
    > > try.)

    >
    > > I would expect that the read would block (I have not set O_NONBLOCK on
    > > the socket) until there is data to be read. Instead, when there is no
    > > data, every read call returns nil. I thought the select would fix
    > > that, but it returns immediately and the read call again returns nil.

    >
    > > Am I misunderstanding how this should work? Would the fact that it's
    > > an SSL connection cause the issue?

    >
    > > I'm using 1.8.4 on Linux.

    >
    > > Thanks for any insights you can offer.

    >
    > > Jeremy

    >
    > Maybe I'm not the best person to be giving advice, but I don't think
    > your do/end is a legal Ruby construction.
    >
    > I was under the impression you would use do/end like the following
    >
    > sock.each_line do |line|
    > puts line
    > end
    >
    > Notice the nifty | | thingies. This will read each line and then
    > return.
    >
    > Chad



    Disregard the post. Your do/end is legal. I think I was thinking of
    something else when I made the comment.
    grocery_stocker, Aug 31, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Claudio
    Replies:
    0
    Views:
    356
    Claudio
    Oct 16, 2003
  2. Tim Black
    Replies:
    1
    Views:
    1,091
    Alan Kennedy
    Aug 3, 2004
  3. Steven
    Replies:
    4
    Views:
    284
    Fredrik Lundh
    Dec 14, 2004
  4. Serge Savoie
    Replies:
    4
    Views:
    262
    Serge Savoie
    Oct 1, 2008
  5. Replies:
    1
    Views:
    278
Loading...

Share This Page