Skip to content
This repository was archived by the owner on Jan 9, 2023. It is now read-only.

Commit 39a3903

Browse files
Merge pull request #139 from Unity-Technologies/bugfix/finish-before-cleanup
- To ensure all transactions are finalized and temp files cleaned up,…
2 parents 9f5cee9 + ece5b82 commit 39a3903

File tree

4 files changed

+254
-1327
lines changed

4 files changed

+254
-1327
lines changed

lib/server/command_processor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ class CommandProcessor extends Duplex {
145145
if(this[kReadStream] === null || this[kSource] === null) return;
146146

147147
let chunk;
148-
while((chunk = this[kReadStream].read()) !== null) {
148+
const rs = this[kReadStream];
149+
while((chunk = rs.read()) !== null) {
149150
this._sendFileQueueChunkReads++;
150151
this._sendFileQueueReadBytes += chunk.length;
151152
if(!this.push(chunk, 'ascii')) break;

lib/server/server.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,13 @@ class CacheServer {
107107
this.errCallback = errCallback;
108108

109109
this._server = net.createServer(socket => {
110-
helpers.log(consts.LOG_INFO, `${socket.remoteAddress}:${socket.remotePort} connected.`);
110+
const remoteAddress = socket.remoteAddress;
111+
const remotePort = socket.remotePort;
112+
113+
helpers.log(consts.LOG_INFO, `${remoteAddress}:${remotePort} connected.`);
111114

112115
const cmdProc = new CommandProcessor(this.cache);
113-
const streamProc = new ClientStreamProcessor({clientAddress: `${socket.remoteAddress}:${socket.remotePort}`});
116+
const streamProc = new ClientStreamProcessor({clientAddress: `${remoteAddress}:${remotePort}`});
114117

115118
const mirrors = this._mirrors;
116119
if(mirrors.length > 0) {
@@ -119,23 +122,32 @@ class CacheServer {
119122
});
120123
}
121124

125+
const unpipeStreams = () => {
126+
socket.unpipe();
127+
streamProc.unpipe();
128+
cmdProc.unpipe();
129+
};
130+
131+
cmdProc.on('finish', () => {
132+
helpers.log(consts.LOG_DBG, `${remoteAddress}:${remotePort} CommandProcessor finished.`);
133+
process.nextTick(unpipeStreams);
134+
});
135+
122136
socket.on('close', () => {
123-
helpers.log(consts.LOG_INFO, `${socket.remoteAddress}:${socket.remotePort} closed connection.`);
124-
socket.unpipe();
125-
streamProc.unpipe();
126-
cmdProc.unpipe();
127-
}).on('error', err => {
128-
helpers.log(consts.LOG_ERR, err.message);
129-
});
137+
helpers.log(consts.LOG_INFO, `${remoteAddress}:${remotePort} Closed connection.`);
138+
}).on('error', err => {
139+
helpers.log(consts.LOG_ERR, `${remoteAddress}:${remotePort} Connection ERROR: ${err.message}`);
140+
unpipeStreams();
141+
});
130142

131-
if(this.isRecordingClient) {
132-
const sessionId = `${socket.remoteAddress}_${socket.remotePort}_${Date.now()}`;
133-
socket.pipe(new ClientStreamRecorder({sessionId})); // Record the incoming byte stream to disk
134-
}
143+
if(this.isRecordingClient) {
144+
const sessionId = `${remoteAddress}_${remotePort}_${Date.now()}`;
145+
socket.pipe(new ClientStreamRecorder({sessionId})); // Record the incoming byte stream to disk
146+
}
135147

136-
socket.pipe(streamProc) // Transform the incoming byte stream into commands and file data
137-
.pipe(cmdProc) // Execute commands and interface with the cache module
138-
.pipe(socket); // Connect back to socket to send files
148+
socket.pipe(streamProc) // Transform the incoming byte stream into commands and file data
149+
.pipe(cmdProc) // Execute commands and interface with the cache module
150+
.pipe(socket); // Connect back to socket to send files
139151
}).on('error', err => {
140152
if (err.code === 'EADDRINUSE') {
141153
helpers.log(consts.LOG_ERR, `Port ${this.port} is already in use...`);

0 commit comments

Comments
 (0)