@@ -80,6 +80,8 @@ var updater = setInterval(function () {
8080 if ( note . server . isDirty ) {
8181 if ( config . debug ) logger . info ( "updater found dirty note: " + key ) ;
8282 updateNote ( note , function ( err , _note ) {
83+ // handle when note already been clean up
84+ if ( ! notes [ key ] || ! notes [ key ] . server ) return callback ( null , null ) ;
8385 if ( ! _note ) {
8486 realtime . io . to ( note . id ) . emit ( 'info' , {
8587 code : 404
@@ -89,7 +91,9 @@ var updater = setInterval(function () {
8991 if ( err || ! _note ) {
9092 for ( var i = 0 , l = note . socks . length ; i < l ; i ++ ) {
9193 var sock = note . socks [ i ] ;
92- sock . disconnect ( true ) ;
94+ if ( typeof sock !== 'undefined' && sock ) {
95+ sock . disconnect ( true ) ;
96+ }
9397 }
9498 return callback ( err , null ) ;
9599 }
@@ -139,6 +143,7 @@ function updateNote(note, callback) {
139143 } ) ;
140144}
141145function finishUpdateNote ( note , _note , callback ) {
146+ if ( ! note || ! note . server ) return callback ( null , null ) ;
142147 var body = note . server . document ;
143148 var title = models . Note . parseNoteTitle ( body ) ;
144149 title = LZString . compressToBase64 ( title ) ;
@@ -329,6 +334,16 @@ function connectNextSocket() {
329334 startConnection ( connectionSocketQueue [ 0 ] ) ;
330335}
331336
337+ function interruptConnection ( socket , note , user ) {
338+ if ( note ) delete note ;
339+ if ( user ) delete user ;
340+ if ( socket )
341+ clearSocketQueue ( connectionSocketQueue , socket ) ;
342+ else
343+ connectionSocketQueue . shift ( ) ;
344+ connectNextSocket ( ) ;
345+ }
346+
332347var isConnectionBusy = false ;
333348var connectionSocketQueue = [ ] ;
334349var isDisconnectBusy = false ;
@@ -337,20 +352,14 @@ var disconnectSocketQueue = [];
337352function finishConnection ( socket , note , user ) {
338353 // if no valid info provided will drop the client
339354 if ( ! socket || ! note || ! user ) {
340- if ( note ) delete note ;
341- if ( user ) delete user ;
342- if ( socket )
343- clearSocketQueue ( connectionSocketQueue , socket ) ;
344- else
345- connectionSocketQueue . shift ( ) ;
346- connectNextSocket ( ) ;
347- return ;
355+ return interruptConnection ( socket , note , user ) ;
348356 }
349357 //check view permission
350358 if ( note . permission == 'private' ) {
351359 if ( socket . request . user && socket . request . user . logged_in && socket . request . user . id == note . owner ) {
352360 //na
353361 } else {
362+ interruptConnection ( socket , note , user ) ;
354363 return failConnection ( 403 , 'connection forbidden' , socket ) ;
355364 }
356365 }
0 commit comments