Fix to make sure we do not copy an element on top of itself.
[m6w6/libmemcached] / libmemcached / memcached_io.c
index 47486d3095b799872a6943004c23821af525c7b9..735a8d5312b625636c80a58e69bc83328fc0589e 100644 (file)
@@ -30,6 +30,17 @@ static memcached_return io_wait(memcached_server_st *ptr,
   fds[0].fd= ptr->fd;
   fds[0].events= flags;
 
+  /*
+  ** We are going to block on write, but at least on Solaris we might block
+  ** on write if we haven't read anything from our input buffer..
+  ** Try to purge the input buffer if we don't do any flow control in the
+  ** application layer (just sending a lot of data etc)
+  ** The test is moved down in the purge function to avoid duplication of
+  ** the test.
+  */
+  if (read_or_write == MEM_WRITE)
+    memcached_purge(ptr);
+
   error= poll(fds, 1, ptr->root->poll_timeout);
 
   if (error == 1)
@@ -124,6 +135,7 @@ ssize_t memcached_io_read(memcached_server_st *ptr,
         }
       }
 
+      ptr->io_bytes_sent = 0;
       ptr->read_data_length= data_read;
       ptr->read_buffer_length= data_read;
       ptr->read_ptr= ptr->read_buffer;
@@ -207,20 +219,27 @@ ssize_t memcached_io_write(memcached_server_st *ptr,
 memcached_return memcached_io_close(memcached_server_st *ptr)
 {
   int r;
-  /* in case of death shutdown to avoid blocking at close() */
 
+  if (ptr->fd == -1)
+    return MEMCACHED_SUCCESS;
+
+  /* in case of death shutdown to avoid blocking at close() */
   r= shutdown(ptr->fd, SHUT_RDWR);
 
 #ifdef HAVE_DEBUG
   if (r && errno != ENOTCONN)
   {
+    WATCHPOINT_NUMBER(ptr->fd);
     WATCHPOINT_ERRNO(errno);
     WATCHPOINT_ASSERT(errno);
   }
 #endif
 
   r= close(ptr->fd);
-  WATCHPOINT_ASSERT(r == 0);
+#ifdef HAVE_DEBUG
+  if (r != 0)
+    WATCHPOINT_ERRNO(errno);
+#endif
 
   return MEMCACHED_SUCCESS;
 }
@@ -280,6 +299,13 @@ static ssize_t io_flush(memcached_server_st *ptr,
     }
     else
     {
+      /*
+      ** We might want to purge the input buffer if we haven't consumed
+      ** any output yet... The test for the limits is the purge is inline
+      ** in the purge function to avoid duplicating the logic..
+      */
+      memcached_purge(ptr);
+
       if ((sent_length= write(ptr->fd, local_write_ptr, 
                                        write_length)) == -1)
       {
@@ -307,13 +333,16 @@ static ssize_t io_flush(memcached_server_st *ptr,
       }
     }
 
+    ptr->io_bytes_sent += sent_length;
+
     local_write_ptr+= sent_length;
     write_length-= sent_length;
     return_length+= sent_length;
   }
 
   WATCHPOINT_ASSERT(write_length == 0);
-  WATCHPOINT_ASSERT(return_length == ptr->write_buffer_offset);
+  // Need to study this assert() WATCHPOINT_ASSERT(return_length ==
+  // ptr->write_buffer_offset);
   ptr->write_buffer_offset= 0;
 
   return return_length;