json output from vis server would fail with incomplete writes

Message ID 1739bfe0907020513m78e06c83s980ce88b5cb5cca5@mail.gmail.com (mailing list archive)
State Superseded, archived
Headers

Commit Message

jonathan mzengeza July 2, 2009, 12:13 p.m. UTC
  Hi

Jonathan here I am attending a code sprint here at CSIR in South Africa.

We are using B.A.T.M.A.N as a mesh networking software. We ran into a bug
which occurred with web browser whereby the  data written back through the
socket was not complete thereby causing an error with our program.

The fix works by making sure that all the data that is to be sent in the
send_buffer is sent. It checks the value of ret until it equals the size of
the data to be sent.

--

             if( ret != strlen( send_buffer ) || (thread_data->format ==
json) )
             {
                 pthread_mutex_lock( &current->mutex );
  

Comments

Marek Lindner July 2, 2009, 11:32 p.m. UTC | #1
Hi,

> We are using B.A.T.M.A.N as a mesh networking software. We ran into a bug
> which occurred with web browser whereby the  data written back through the
> socket was not complete thereby causing an error with our program.
>
> The fix works by making sure that all the data that is to be sent in the
> send_buffer is sent. It checks the value of ret until it equals the size of
> the data to be sent.

thanks for your patch. This spot you found really is not that beautiful.  ;-)
Looking at your patch I ask myself what will happen if the other end dies in 
the process ? You are in a loop that constantly checks if all data has been 
transmitted and retries to send without checking for timeout errors or 
connection problems. I have the feeling it could lead to an endless loop. I 
might be wrong - what do you think ?
Also, I never used fsync on a socket. It is doing the same thing as on a file ? 
What about using the SIOCOUTQ ioctl (which is linux specific and not 
portable) ?

Regards,
Marek
  

Patch

Index: vis.c
===================================================================
--- vis.c    (revision 1336)
+++ vis.c    (working copy)
@@ -580,8 +580,18 @@ 
             } else {
                 send_buffer = current->json_buffer;
             }
+
+            ret = write( thread_data->socket, send_buffer, strlen(
send_buffer ));

-            ret = write( thread_data->socket, send_buffer, strlen(
send_buffer ) );
+            while(ret != strlen( send_buffer )) {
+                send_buffer += ret;
+
+                if ( fsync(thread_data->socket) != 0 ) {
+                    ret = write(thread_data->socket, send_buffer, strlen(
send_buffer ));
+                }
+
+            }
+