Browse code

tcp: more consistent IO_FD_CLOSING usage

- extra safety checks before using IO_FD_CLOSING in io_watch_del()
calls (must be used only when the fd will be close() imediately
afterwards).
- add IO_FD_CLOSING when POLLERR or POLLHUP is detected in
tcp_main and the socket receive buffer is empty (in this case
the fd will be shortly closed)

Andrei Pelinescu-Onciul authored on 18/06/2010 22:21:58
Showing 1 changed files
... ...
@@ -3099,18 +3099,31 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
3099 3099
 		case CONN_RELEASE:
3100 3100
 			tcp_c->busy--;
3101 3101
 			if (unlikely(tcpconn_put(tcpconn))){
3102
+				/* if refcnt was 1 => it was used only in the
3103
+				   tcp reader => it's not hashed or watched for IO
3104
+				   anymore => no need to io_watch_del() */
3102 3105
 				tcpconn_destroy(tcpconn);
3103 3106
 				break;
3104 3107
 			}
3105 3108
 			if (unlikely(tcpconn->state==S_CONN_BAD)){ 
3109
+				if (tcpconn_try_unhash(tcpconn)) {
3106 3110
 #ifdef TCP_ASYNC
3107
-				if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
3108
-					io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
3111
+					if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
3112
+						io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
3113
+						tcpconn->flags &= ~F_CONN_WRITE_W;
3114
+					}
3115
+#endif /* TCP_ASYNC */
3116
+					tcpconn_put_destroy(tcpconn);
3117
+				}
3118
+#ifdef TCP_ASYNC
3119
+				 else if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
3120
+					/* should never happen: if it's already unhashed, it
3121
+					   should not be watched for IO */
3122
+					BUG("unhashed connection watched for write\n");
3123
+					io_watch_del(&io_h, tcpconn->s, -1, 0);
3109 3124
 					tcpconn->flags &= ~F_CONN_WRITE_W;
3110 3125
 				}
3111 3126
 #endif /* TCP_ASYNC */
3112
-				if (tcpconn_try_unhash(tcpconn))
3113
-					tcpconn_put_destroy(tcpconn);
3114 3127
 				break;
3115 3128
 			}
3116 3129
 			/* update the timeout*/
... ...
@@ -3147,12 +3160,17 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
3147 3160
 						TCP_EV_SEND_TIMEOUT(0, &tcpconn->rcv);
3148 3161
 						TCP_STATS_SEND_TIMEOUT();
3149 3162
 					}
3150
-					if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
3151
-						io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
3163
+					if (tcpconn_try_unhash(tcpconn)) {
3164
+						if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
3165
+							io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
3166
+							tcpconn->flags&=~F_CONN_WRITE_W;
3167
+						}
3168
+						tcpconn_put_destroy(tcpconn);
3169
+					} else if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
3170
+						BUG("unhashed connection watched for write\n");
3171
+						io_watch_del(&io_h, tcpconn->s, -1, 0);
3152 3172
 						tcpconn->flags&=~F_CONN_WRITE_W;
3153 3173
 					}
3154
-					if (tcpconn_try_unhash(tcpconn))
3155
-						tcpconn_put_destroy(tcpconn);
3156 3174
 					break;
3157 3175
 				}else{
3158 3176
 					crt_timeout=MIN_unsigned(con_lifetime,
... ...
@@ -3177,14 +3195,22 @@ inline static int handle_tcp_child(struct tcp_child* tcp_c, int fd_i)
3177 3195
 				LOG(L_CRIT, "ERROR: tcp_main: handle_tcp_child: failed to add"
3178 3196
 						" new socket to the fd list\n");
3179 3197
 				tcpconn->flags&=~F_CONN_READ_W;
3198
+				if (tcpconn_try_unhash(tcpconn)) {
3180 3199
 #ifdef TCP_ASYNC
3181
-				if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
3182
-					io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
3200
+					if (unlikely(tcpconn->flags & F_CONN_WRITE_W)){
3201
+						io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
3202
+						tcpconn->flags&=~F_CONN_WRITE_W;
3203
+					}
3204
+#endif /* TCP_ASYNC */
3205
+					tcpconn_put_destroy(tcpconn);
3206
+				}
3207
+#ifdef TCP_ASYNC
3208
+				 else if (unlikely(tcpconn->flags & F_CONN_WRITE_W)) {
3209
+					BUG("unhashed connection watched for write\n");
3210
+					io_watch_del(&io_h, tcpconn->s, -1, 0);
3183 3211
 					tcpconn->flags&=~F_CONN_WRITE_W;
3184 3212
 				}
3185 3213
 #endif /* TCP_ASYNC */
3186
-				if (tcpconn_try_unhash(tcpconn))
3187
-					tcpconn_put_destroy(tcpconn);
3188 3214
 				break;
3189 3215
 			}
3190 3216
 			DBG("handle_tcp_child: CONN_RELEASE  %p refcnt= %d\n", 
... ...
@@ -3457,10 +3483,16 @@ inline static int handle_ser_child(struct process_table* p, int fd_i)
3457 3483
 													POLLIN|POLLOUT, -1)<0)){
3458 3484
 							LOG(L_CRIT, "ERROR: tcp_main: handle_ser_child:"
3459 3485
 									" failed to change socket watch events\n");
3460
-							io_watch_del(&io_h, tcpconn->s, -1, IO_FD_CLOSING);
3461
-							tcpconn->flags&=~F_CONN_READ_W;
3462
-							if (tcpconn_try_unhash(tcpconn))
3486
+							if (tcpconn_try_unhash(tcpconn)) {
3487
+								io_watch_del(&io_h, tcpconn->s, -1,
3488
+												IO_FD_CLOSING);
3489
+								tcpconn->flags&=~F_CONN_READ_W;
3463 3490
 								tcpconn_put_destroy(tcpconn);
3491
+							} else {
3492
+								BUG("unhashed connection watched for IO\n");
3493
+								io_watch_del(&io_h, tcpconn->s, -1, 0);
3494
+								tcpconn->flags&=~F_CONN_READ_W;
3495
+							}
3464 3496
 							break;
3465 3497
 						}
3466 3498
 					}
... ...
@@ -3782,10 +3814,6 @@ inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, short ev,
3782 3814
 					(wbufq_run(tcpconn->s, tcpconn, &empty_q)<0) ||
3783 3815
 					(empty_q && tcpconn_close_after_send(tcpconn))
3784 3816
 			)){
3785
-			if (unlikely(io_watch_del(&io_h, tcpconn->s, fd_i, 0)<0)){
3786
-				LOG(L_ERR, "ERROR: handle_tcpconn_ev: io_watch_del(1) failed:"
3787
-							" for %p, fd %d\n", tcpconn, tcpconn->s);
3788
-			}
3789 3817
 			if ((tcpconn->flags & F_CONN_READ_W) && (ev & POLLIN)){
3790 3818
 				/* connection is watched for read and there is a read event
3791 3819
 				 * (unfortunately if we have POLLIN here we don't know if 
... ...
@@ -3798,6 +3826,11 @@ inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, short ev,
3798 3826
 				 * conn.  to a a child only if needed (another syscall + at 
3799 3827
 				 * least 2 * syscalls in the reader + ...) */
3800 3828
 				if ((ioctl(tcpconn->s, FIONREAD, &bytes)>=0) && (bytes>0)){
3829
+					if (unlikely(io_watch_del(&io_h, tcpconn->s, fd_i, 0)<0)){
3830
+						LOG(L_ERR, "ERROR: handle_tcpconn_ev: io_watch_del(1)"
3831
+								" failed: for %p, fd %d\n",
3832
+								tcpconn, tcpconn->s);
3833
+					}
3801 3834
 					tcpconn->flags&=~(F_CONN_WRITE_W|F_CONN_READ_W|
3802 3835
 										F_CONN_WANTS_RD|F_CONN_WANTS_WR);
3803 3836
 					tcpconn->flags|=F_CONN_FORCE_EOF|F_CONN_WR_ERROR;
... ...
@@ -3805,6 +3838,11 @@ inline static int handle_tcpconn_ev(struct tcp_connection* tcpconn, short ev,
3805 3838
 				}
3806 3839
 				/* if bytes==0 or ioctl failed, destroy the connection now */
3807 3840
 			}
3841
+			if (unlikely(io_watch_del(&io_h, tcpconn->s, fd_i,
3842
+											IO_FD_CLOSING) < 0)){
3843
+				LOG(L_ERR, "ERROR: handle_tcpconn_ev: io_watch_del() failed:"
3844
+							" for %p, fd %d\n", tcpconn, tcpconn->s);
3845
+			}
3808 3846
 			tcpconn->flags&=~(F_CONN_WRITE_W|F_CONN_READ_W|
3809 3847
 								F_CONN_WANTS_RD|F_CONN_WANTS_WR);
3810 3848
 			if (unlikely(ev & POLLERR)){
... ...
@@ -3898,7 +3936,8 @@ send_to_child:
3898 3936
 			tcpconn->flags&=~F_CONN_READER;
3899 3937
 #ifdef TCP_ASYNC
3900 3938
 			if (tcpconn->flags & F_CONN_WRITE_W){
3901
-				if (unlikely(io_watch_del(&io_h, tcpconn->s, fd_i, 0)<0)){
3939
+				if (unlikely(io_watch_del(&io_h, tcpconn->s, fd_i,
3940
+														IO_FD_CLOSING) < 0)){
3902 3941
 					LOG(L_ERR, "ERROR: handle_tcpconn_ev: io_watch_del(4)"
3903 3942
 							" failed:" " for %p, fd %d\n",
3904 3943
 							tcpconn, tcpconn->s);