title: How should I handle blocking operations when using scala actors?
body: <p>I started learning the scala actors framework about two days ago. To make the ideas concrete in my mind, I decided to implement a TCP based echo server that could handle multiple simultaneous connections.</p>
  
  <p>Here is the code for the echo server (error handling not included):</p>
  
  <pre><code>class EchoServer extends Actor {
    private var connections = 0
  
    def act() {
      val serverSocket = new ServerSocket(6789)
  
      val echoServer = self
      actor { while (true) echoServer ! ("Connected", serverSocket.accept) }
  
      while (true) {
        receive {
          case ("Connected", connectionSocket: Socket) =&gt;
            connections += 1
            (new ConnectionHandler(this, connectionSocket)).start
          case "Disconnected" =&gt;
            connections -= 1
        }
      }
    }
  }
  </code></pre>
  
  <p>Basically, the server is an Actor that handles the "Connected" and "Disconnected" messages. It delegates the connection listening to an anonymous actor that invokes the <strong>accept()</strong> method (a blocking operation) on the <strong>serverSocket</strong>. When a connection arrives it informs the server via the "Connected" message and passes it the socket to use for communication with the newly connected client. An instance of the <strong>ConnectionHandler</strong> class handles the actual communication with the client.</p>
  
  <p>Here is the code for the connection handler (some error handling included):</p>
  
  <pre><code>class ConnectionHandler(server: EchoServer, connectionSocket: Socket)
      extends Actor {
  
    def act() {
      for (input &lt;- getInputStream; output &lt;- getOutputStream) {
        val handler = self
        actor {
          var continue = true
          while (continue) {
            try {
              val req = input.readLine
              if (req != null) handler ! ("Request", req)
              else continue = false
            } catch {
              case e: IOException =&gt; continue = false
            }
          }
  
          handler ! "Disconnected"
        }
  
        var connected = true
        while (connected) {
          receive {
            case ("Request", req: String) =&gt;
              try {
                output.writeBytes(req + "\n")
              } catch {
                case e: IOException =&gt; connected = false
              }
            case "Disconnected" =&gt;
              connected = false
          }
        }
      }
  
      close()
      server ! "Disconnected"
    }
  
    // code for getInputStream(), getOutputStream() and close() methods
  }
  </code></pre>
  
  <p>The connection handler uses an anonymous actor that waits for requests to be sent to the socket by calling the <strong>readLine()</strong> method (a blocking operation) on the input stream of the socket. When a request is received a "Request" message is sent to the handler which then simply echoes the request back to the client. If the handler or the anonymous actor experiences problems with the underlying socket then the socket is closed and a "Disconnect" message is sent to the echo server indicating that the client has been disconnected from the server.</p>
  
  <p>So, I can fire up the echo server and let it wait for connections. Then I can open a new terminal and connect to the server via telnet. I can send it requests and it responds correctly. Now, if I open another terminal and connect to the server the server registers the connection but fails to start the connection handler for this new connection. When I send it messages via any of the existing connections I get no immediate response. Here's the interesting part. When I terminate all but one of the existing client connections and leave client X open, then all the responses to the request I sent via client X are returned. I've done some tests and concluded that the <strong>act()</strong> method is not being called on subsequent client connections even though I call the <strong>start()</strong> method on creating the connection handler.</p>
  
  <p>I suppose I'm handling the blocking operations incorrectly in my connection handler. Since a previous connection is handled by a connection handler that has an anonymous actor blocked waiting for a request I'm thinking that this blocked actor is preventing the other actors (connection handlers) from starting up.</p>
  
  <p>How should I handle blocking operations when using scala actors?</p>
  
  <p>Any help would be greatly appreciated.</p>