Skip to contentSkip to conversation

Delete IOHandlers after potentially running them

PATCH Applied Stefan Hajnoczi 5 participants 10 messages Nov 3, 2010 – Nov 16, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Delete IOHandlers after potentially running them

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

---
 vl.c |   15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)
vl.c +8 -7
index 7038952..6f56123 100644
@@ -1252,17 +1252,18 @@ void main_loop_wait(int nonblocking)
         IOHandlerRecord *pioh;
 
         QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
-            if (ioh->deleted) {
-                QLIST_REMOVE(ioh, next);
-                qemu_free(ioh);
-                continue;
-            }
-            if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
+            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
                 ioh->fd_read(ioh->opaque);
             }
-            if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
+            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
                 ioh->fd_write(ioh->opaque);
             }
+
+            /* Do this last in case read/write handlers marked it for deletion */
+            if (ioh->deleted) {
+                QLIST_REMOVE(ioh, next);
+                qemu_free(ioh);
+            }
         }
     }
 
Signature · 1 line
1.7.2.3
#1 lore raw
Quoted 6 lines · Stefan Hajnoczi

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

Reviewed by: Juan Quintela <[email protected]>

But once here, what (appart from networking) reads and writes to the same fd? And that removes the IOHandler on read while there are write stuff pending?

Later, Juan.

Quoted 34 lines
---
 vl.c |   15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)
vl.c +8 -7
index 7038952..6f56123 100644
@@ -1252,17 +1252,18 @@ void main_loop_wait(int nonblocking)
         IOHandlerRecord *pioh;
 
         QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
-            if (ioh->deleted) {
-                QLIST_REMOVE(ioh, next);
-                qemu_free(ioh);
-                continue;
-            }
-            if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
+            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
                 ioh->fd_read(ioh->opaque);
             }
-            if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
+            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
                 ioh->fd_write(ioh->opaque);
             }
+
+            /* Do this last in case read/write handlers marked it for deletion */
+            if (ioh->deleted) {
+                QLIST_REMOVE(ioh, next);
+                qemu_free(ioh);
+            }
         }
     }
#2 lore raw
Quoted 5 lines · Juan Quintela
Quoted 4 lines · Stefan Hajnoczi

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

Quoted 8 lines
Quoted 2 lines

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

Reviewed by: Juan Quintela <[email protected]>

But once here, what (appart from networking) reads and writes to the same fd?  And that removes the IOHandler on read while there are write stuff pending?

VNC

Stefan

#3 lore raw
In reply to Stefan Hajnoczi
Quoted 31 lines · Stefan Hajnoczi

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

---
  vl.c |   15 ++++++++-------
  1 files changed, 8 insertions(+), 7 deletions(-)
vl.c +2 -7
index 7038952..6f56123 100644
@@ -1252,17 +1252,18 @@ void main_loop_wait(int nonblocking)
          IOHandlerRecord *pioh;
 
          QLIST_FOREACH_SAFE(ioh,&io_handlers, next, pioh) {
-            if (ioh->deleted) {
-                QLIST_REMOVE(ioh, next);
-                qemu_free(ioh);
-                continue;
-            }
-            if (ioh->fd_read&&  FD_ISSET(ioh->fd,&rfds)) {
+            if (!ioh->deleted&&  ioh->fd_read&&  FD_ISSET(ioh->fd,&rfds)) {
                  ioh->fd_read(ioh->opaque);
              }
-            if (ioh->fd_write&&  FD_ISSET(ioh->fd,&wfds)) {
+            if (!ioh->deleted&&  ioh->fd_write&&  FD_ISSET(ioh->fd,&wfds)) {

vl.c void main_loop_wait(int nonblocking) · line 1268

Quoted 10 lines
vl.c +6 -0
                  ioh->fd_write(ioh->opaque);
              }
+
+            /* Do this last in case read/write handlers marked it for deletion */
+            if (ioh->deleted) {
+                QLIST_REMOVE(ioh, next);
+                qemu_free(ioh);
+            }
          }
 

This isn't enough. If you end up with a handler deleting the next pointer and the current pointer, you'll end up running off the end of the list.

The original commit should be reverted.

Regards,

Anthony Liguori

Quoted 3 lines
vl.c +0 -0
      }
#4 lore raw
Quoted 38 lines · Anthony Liguori
Quoted 37 lines · Stefan Hajnoczi

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

---
  vl.c |   15 ++++++++-------
  1 files changed, 8 insertions(+), 7 deletions(-)
vl.c +6 -7
index 7038952..6f56123 100644
@@ -1252,17 +1252,18 @@ void main_loop_wait(int nonblocking)
          IOHandlerRecord *pioh;
 
          QLIST_FOREACH_SAFE(ioh,&io_handlers, next, pioh) {
-            if (ioh->deleted) {
-                QLIST_REMOVE(ioh, next);
-                qemu_free(ioh);
-                continue;
-            }
-            if (ioh->fd_read&&  FD_ISSET(ioh->fd,&rfds)) {
+            if (!ioh->deleted&&  ioh->fd_read&&  FD_ISSET(ioh->fd,&rfds)) {
                  ioh->fd_read(ioh->opaque);
              }
-            if (ioh->fd_write&&  FD_ISSET(ioh->fd,&wfds)) {
+            if (!ioh->deleted&&  ioh->fd_write&&  FD_ISSET(ioh->fd,&wfds)) {
                  ioh->fd_write(ioh->opaque);
              }
+
+            /* Do this last in case read/write handlers marked it for deletion */
+            if (ioh->deleted) {
+                QLIST_REMOVE(ioh, next);
Quoted 8 lines

vl.c void main_loop_wait(int nonblocking) · line 1269

Quoted 4 lines
vl.c +2 -0
+                qemu_free(ioh);
+            }
          }
 

This isn't enough. If you end up with a handler deleting the next pointer and the current pointer, you'll end up running off the end of the list.

What is the point of that?

That a handler can remove itself is ok.
But that a handler can remove also the next in a list that is used for other things looks pretty insane to me.

Quoted 1 line

The original commit should be reverted.

If that behaviour is expected, then I agree that we should revert it. But I would consider that behaviour wrong.

Later, Juan.

Quoted 7 lines

Regards,

Anthony Liguori

Quoted 3 lines
vl.c +0 -0
      }
#5 lore raw
Quoted 50 lines · Juan Quintela
Quoted 39 lines · Anthony Liguori
Quoted 37 lines · Stefan Hajnoczi

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

---
   vl.c |   15 ++++++++-------
   1 files changed, 8 insertions(+), 7 deletions(-)
vl.c +6 -7
index 7038952..6f56123 100644
@@ -1252,17 +1252,18 @@ void main_loop_wait(int nonblocking)
           IOHandlerRecord *pioh;
 
           QLIST_FOREACH_SAFE(ioh,&io_handlers, next, pioh) {
-            if (ioh->deleted) {
-                QLIST_REMOVE(ioh, next);
-                qemu_free(ioh);
-                continue;
-            }
-            if (ioh->fd_read&&   FD_ISSET(ioh->fd,&rfds)) {
+            if (!ioh->deleted&&   ioh->fd_read&&   FD_ISSET(ioh->fd,&rfds)) {
                   ioh->fd_read(ioh->opaque);
               }
-            if (ioh->fd_write&&   FD_ISSET(ioh->fd,&wfds)) {
+            if (!ioh->deleted&&   ioh->fd_write&&   FD_ISSET(ioh->fd,&wfds)) {
                   ioh->fd_write(ioh->opaque);
               }
+
+            /* Do this last in case read/write handlers marked it for deletion */
+            if (ioh->deleted) {
+                QLIST_REMOVE(ioh, next);
Quoted 9 lines

vl.c void main_loop_wait(int nonblocking)

Quoted 5 lines
vl.c +2 -0
+                qemu_free(ioh);
+            }
           }
 
 

This isn't enough. If you end up with a handler deleting the next pointer and the current pointer, you'll end up running off the end of the list.

Quoted 6 lines

What is the point of that?

That a handler can remove itself is ok.
But that a handler can remove also the next in a list that is used for other things looks pretty insane to me.

If you have multiple file descriptors registered for something and you get an EOF on one of the file descriptors, your clean-up action that happens as a result of closing the session may involve deleting more than one file descriptor callback.

Regards,

Anthony Liguori

Quoted 17 lines
Quoted 2 lines

The original commit should be reverted.

If that behaviour is expected, then I agree that we should revert it. But I would consider that behaviour wrong.

Later, Juan.

Quoted 9 lines

Regards,

Anthony Liguori

Quoted 4 lines
       }
#6 lore raw
Quoted 58 lines · Anthony Liguori
Quoted 50 lines · Juan Quintela
Quoted 39 lines · Anthony Liguori
Quoted 37 lines · Stefan Hajnoczi

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

---
   vl.c |   15 ++++++++-------
   1 files changed, 8 insertions(+), 7 deletions(-)
vl.c +6 -7
index 7038952..6f56123 100644
@@ -1252,17 +1252,18 @@ void main_loop_wait(int nonblocking)
           IOHandlerRecord *pioh;
 
           QLIST_FOREACH_SAFE(ioh,&io_handlers, next, pioh) {
-            if (ioh->deleted) {
-                QLIST_REMOVE(ioh, next);
-                qemu_free(ioh);
-                continue;
-            }
-            if (ioh->fd_read&&   FD_ISSET(ioh->fd,&rfds)) {
+            if (!ioh->deleted&&   ioh->fd_read&&   FD_ISSET(ioh->fd,&rfds)) {
                   ioh->fd_read(ioh->opaque);
               }
-            if (ioh->fd_write&&   FD_ISSET(ioh->fd,&wfds)) {
+            if (!ioh->deleted&&   ioh->fd_write&&   FD_ISSET(ioh->fd,&wfds)) {
                   ioh->fd_write(ioh->opaque);
               }
+
+            /* Do this last in case read/write handlers marked it for deletion */
+            if (ioh->deleted) {
+                QLIST_REMOVE(ioh, next);
Quoted 9 lines

vl.c void main_loop_wait(int nonblocking)

Quoted 5 lines
vl.c +2 -0
+                qemu_free(ioh);
+            }
           }
 
 

This isn't enough. If you end up with a handler deleting the next pointer and the current pointer, you'll end up running off the end of the list.

Quoted 6 lines

What is the point of that?

That a handler can remove itself is ok.
But that a handler can remove also the next in a list that is used for other things looks pretty insane to me.

Quoted 4 lines

If you have multiple file descriptors registered for something and you get an EOF on one of the file descriptors, your clean-up action that happens as a result of closing the session may involve deleting more than one file descriptor callback.

But that is completely wrong. you just put an ioh->deleted=1 for the others, and you are right, no?

Later, Juan.

#7 lore raw
Quoted 59 lines · Juan Quintela
Quoted 58 lines · Anthony Liguori
Quoted 56 lines · Juan Quintela
Quoted 39 lines · Anthony Liguori
Quoted 37 lines · Stefan Hajnoczi

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

---
   vl.c |   15 ++++++++-------

   1 files changed, 8 insertions(+), 7 deletions(-)
vl.c +6 -7
index 7038952..6f56123 100644
@@ -1252,17 +1252,18 @@ void main_loop_wait(int nonblocking)
            IOHandlerRecord *pioh;
 
            QLIST_FOREACH_SAFE(ioh,&io_handlers, next, pioh) {
-            if (ioh->deleted) {
-                QLIST_REMOVE(ioh, next);
-                qemu_free(ioh);
-                continue;
-            }
-            if (ioh->fd_read&&   FD_ISSET(ioh->fd,&rfds)) {
+            if (!ioh->deleted&&   ioh->fd_read&&   FD_ISSET(ioh->fd,&rfds)) {
                    ioh->fd_read(ioh->opaque);
                }
-            if (ioh->fd_write&&   FD_ISSET(ioh->fd,&wfds)) {
+            if (!ioh->deleted&&   ioh->fd_write&&   FD_ISSET(ioh->fd,&wfds)) {
                    ioh->fd_write(ioh->opaque);
                }
+
+            /* Do this last in case read/write handlers marked it for deletion */
+            if (ioh->deleted) {
+                QLIST_REMOVE(ioh, next);
Quoted 9 lines

vl.c void main_loop_wait(int nonblocking)

Quoted 5 lines
vl.c +2 -0
+                qemu_free(ioh);
+            }
            }
 
 

This isn't enough.  If you end up with a handler deleting the next pointer and the current pointer, you'll end up running off the end of the list.

What is the point of that?

That a handler can remove itself is ok.
But that a handler can remove also the next in a list that is used for other things looks pretty insane to me.

Quoted 7 lines
Quoted 4 lines

If you have multiple file descriptors registered for something and you get an EOF on one of the file descriptors, your clean-up action that happens as a result of closing the session may involve deleting more than one file descriptor callback.

But that is completely wrong.  you just put an ioh->deleted=1 for the others, and you are right, no?

Yes, other IOHandlerRecords will not be removed from the list yet, they will only be marked as ioh->deleted=1.

Anthony, are you happy to merge this patch?

Stefan

#8 lore raw
In reply to Stefan Hajnoczi
Quoted 30 lines · Stefan Hajnoczi

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

---
  vl.c |   15 ++++++++-------
  1 files changed, 8 insertions(+), 7 deletions(-)
vl.c +1 -7
index 7038952..6f56123 100644
@@ -1252,17 +1252,18 @@ void main_loop_wait(int nonblocking)
          IOHandlerRecord *pioh;
 
          QLIST_FOREACH_SAFE(ioh,&io_handlers, next, pioh) {
-            if (ioh->deleted) {
-                QLIST_REMOVE(ioh, next);
-                qemu_free(ioh);
-                continue;
-            }
-            if (ioh->fd_read&&  FD_ISSET(ioh->fd,&rfds)) {
+            if (!ioh->deleted&&  ioh->fd_read&&  FD_ISSET(ioh->fd,&rfds)) {
                  ioh->fd_read(ioh->opaque);
              }
-            if (ioh->fd_write&&  FD_ISSET(ioh->fd,&wfds)) {

vl.c void main_loop_wait(int nonblocking) · line 1267

Quoted 10 lines
vl.c +7 -0
+            if (!ioh->deleted&&  ioh->fd_write&&  FD_ISSET(ioh->fd,&wfds)) {
                  ioh->fd_write(ioh->opaque);
              }
+
+            /* Do this last in case read/write handlers marked it for deletion */
+            if (ioh->deleted) {
+                QLIST_REMOVE(ioh, next);
+                qemu_free(ioh);
+            }
 

Actually, on second thought, I think this is correct although I still think reverting is a better strategy.

Regards,

Anthony Liguori

Quoted 4 lines
vl.c +0 -0
          }
      }
#9 lore raw
In reply to Stefan Hajnoczi
Quoted 9 lines · Stefan Hajnoczi

Since commit 4bed9837309e58d208183f81d8344996744292cf an .fd_read() handler that deletes its IOHandler is exposed to .fd_write() being called on the deleted IOHandler.

This patch fixes deletion so that .fd_read() and .fd_write() are never called on an IOHandler that is marked for deletion.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 35 lines
---
  vl.c |   15 ++++++++-------
  1 files changed, 8 insertions(+), 7 deletions(-)
vl.c +8 -7
index 7038952..6f56123 100644
@@ -1252,17 +1252,18 @@ void main_loop_wait(int nonblocking)
          IOHandlerRecord *pioh;
 
          QLIST_FOREACH_SAFE(ioh,&io_handlers, next, pioh) {
-            if (ioh->deleted) {
-                QLIST_REMOVE(ioh, next);
-                qemu_free(ioh);
-                continue;
-            }
-            if (ioh->fd_read&&  FD_ISSET(ioh->fd,&rfds)) {
+            if (!ioh->deleted&&  ioh->fd_read&&  FD_ISSET(ioh->fd,&rfds)) {
                  ioh->fd_read(ioh->opaque);
              }
-            if (ioh->fd_write&&  FD_ISSET(ioh->fd,&wfds)) {
+            if (!ioh->deleted&&  ioh->fd_write&&  FD_ISSET(ioh->fd,&wfds)) {
                  ioh->fd_write(ioh->opaque);
              }
+
+            /* Do this last in case read/write handlers marked it for deletion */
+            if (ioh->deleted) {
+                QLIST_REMOVE(ioh, next);
+                qemu_free(ioh);
+            }
          }
      }