Skip to content

Commit cb74c58

Browse files
Merge pull request #807 from DustinCampbell/fix-omnisharp-launchNix
Don't immediately return promise within newly created promise
2 parents af0f728 + a880c20 commit cb74c58

File tree

2 files changed

+61
-76
lines changed

2 files changed

+61
-76
lines changed

src/omnisharp/launcher.ts

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,15 @@ export interface LaunchResult {
161161

162162
export function launchOmniSharp(details: LaunchDetails): Promise<LaunchResult> {
163163
return new Promise<LaunchResult>((resolve, reject) => {
164-
try {
165-
return launch(details).then(result => {
166-
// async error - when target not not ENEOT
167-
result.process.on('error', reject);
168-
169-
// success after a short freeing event loop
170-
setTimeout(function () {
171-
resolve(result);
172-
}, 0);
173-
}, err => {
174-
reject(err);
175-
});
176-
} catch (err) {
177-
reject(err);
178-
}
164+
launch(details).then(result => {
165+
// async error - when target not not ENEOT
166+
result.process.on('error', reject);
167+
168+
// success after a short freeing event loop
169+
setTimeout(function () {
170+
resolve(result);
171+
}, 0);
172+
});
179173
});
180174
}
181175

@@ -189,34 +183,31 @@ function launch(details: LaunchDetails): Promise<LaunchResult> {
189183
}
190184

191185
function launchWindows(details: LaunchDetails): Promise<LaunchResult> {
192-
return new Promise<LaunchResult>(resolve => {
193-
194-
function escapeIfNeeded(arg: string) {
195-
const hasSpaceWithoutQuotes = /^[^"].* .*[^"]/;
196-
return hasSpaceWithoutQuotes.test(arg)
197-
? `"${arg}"`
198-
: arg;
199-
}
186+
function escapeIfNeeded(arg: string) {
187+
const hasSpaceWithoutQuotes = /^[^"].* .*[^"]/;
188+
return hasSpaceWithoutQuotes.test(arg)
189+
? `"${arg}"`
190+
: arg;
191+
}
200192

201-
let args = details.args.slice(0); // create copy of details.args
202-
args.unshift(details.serverPath);
203-
args = [[
204-
'/s',
205-
'/c',
206-
'"' + args.map(escapeIfNeeded).join(' ') + '"'
207-
].join(' ')];
208-
209-
let process = spawn('cmd', args, <any>{
210-
windowsVerbatimArguments: true,
211-
detached: false,
212-
cwd: details.cwd
213-
});
193+
let args = details.args.slice(0); // create copy of details.args
194+
args.unshift(details.serverPath);
195+
args = [[
196+
'/s',
197+
'/c',
198+
'"' + args.map(escapeIfNeeded).join(' ') + '"'
199+
].join(' ')];
200+
201+
let process = spawn('cmd', args, <any>{
202+
windowsVerbatimArguments: true,
203+
detached: false,
204+
cwd: details.cwd
205+
});
214206

215-
return resolve({
216-
process,
217-
command: details.serverPath,
218-
usingMono: false
219-
});
207+
return Promise.resolve({
208+
process,
209+
command: details.serverPath,
210+
usingMono: false
220211
});
221212
}
222213

@@ -233,48 +224,44 @@ function launchNix(details: LaunchDetails): Promise<LaunchResult> {
233224
}
234225

235226
function launchNixCoreCLR(details: LaunchDetails): Promise<LaunchResult> {
236-
return new Promise<LaunchResult>(resolve => {
237-
let process = spawn(details.serverPath, details.args, {
238-
detached: false,
239-
cwd: details.cwd
240-
});
227+
let process = spawn(details.serverPath, details.args, {
228+
detached: false,
229+
cwd: details.cwd
230+
});
241231

242-
return resolve({
243-
process,
244-
command: details.serverPath,
245-
usingMono: false
246-
});
232+
return Promise.resolve({
233+
process,
234+
command: details.serverPath,
235+
usingMono: false
247236
});
248237
}
249238

250239
function launchNixMono(details: LaunchDetails): Promise<LaunchResult> {
251-
return new Promise<LaunchResult>((resolve, reject) => {
252-
return canLaunchMono().then(() => {
253-
let args = details.args.slice(0); // create copy of details.args
254-
args.unshift(details.serverPath);
255-
256-
let process = spawn('mono', args, {
257-
detached: false,
258-
cwd: details.cwd
259-
});
240+
return canLaunchMono().then(() => {
241+
let args = details.args.slice(0); // create copy of details.args
242+
args.unshift(details.serverPath);
260243

261-
return resolve({
262-
process,
263-
command: details.serverPath,
264-
usingMono: true
265-
});
244+
let process = spawn('mono', args, {
245+
detached: false,
246+
cwd: details.cwd
266247
});
248+
249+
return {
250+
process,
251+
command: details.serverPath,
252+
usingMono: true
253+
};
267254
});
268255
}
269256

270257
function canLaunchMono(): Promise<void> {
271258
return new Promise<void>((resolve, reject) => {
272259
hasMono('>=4.0.1').then(success => {
273260
if (success) {
274-
return resolve();
261+
resolve();
275262
}
276263
else {
277-
return reject(new Error('Cannot start Omnisharp because Mono version >=4.0.1 is required.'));
264+
reject(new Error('Cannot start Omnisharp because Mono version >=4.0.1 is required.'));
278265
}
279266
});
280267
});

src/omnisharp/server.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,9 @@ export abstract class OmnisharpServer {
299299
this._telemetryIntervalId = setInterval(() => this._reportTelemetry(), TelemetryReportingDelay);
300300
}).then(() => {
301301
this._processQueue();
302-
}, err => {
302+
}).catch(err => {
303303
this._fireEvent(Events.ServerError, err);
304-
this._setState(ServerState.Stopped);
305-
throw err;
304+
return this.stop();
306305
});
307306
});
308307
}
@@ -311,7 +310,7 @@ export abstract class OmnisharpServer {
311310

312311
public stop(): Promise<void> {
313312

314-
let ret: Promise<void>;
313+
let cleanupPromise: Promise<void>;
315314

316315
if (this._telemetryIntervalId !== undefined) {
317316
// Stop reporting telemetry
@@ -322,13 +321,13 @@ export abstract class OmnisharpServer {
322321

323322
if (!this._serverProcess) {
324323
// nothing to kill
325-
ret = Promise.resolve();
324+
cleanupPromise = Promise.resolve();
326325
}
327326
else if (process.platform === 'win32') {
328327
// when killing a process in windows its child
329328
// processes are *not* killed but become root
330329
// processes. Therefore we use TASKKILL.EXE
331-
ret = new Promise<void>((resolve, reject) => {
330+
cleanupPromise = new Promise<void>((resolve, reject) => {
332331
const killer = exec(`taskkill /F /T /PID ${this._serverProcess.pid}`, (err, stdout, stderr) => {
333332
if (err) {
334333
return reject(err);
@@ -342,14 +341,13 @@ export abstract class OmnisharpServer {
342341
else {
343342
// Kill Unix process
344343
this._serverProcess.kill('SIGTERM');
345-
ret = Promise.resolve();
344+
cleanupPromise = Promise.resolve();
346345
}
347346

348-
return ret.then(() => {
347+
return cleanupPromise.then(() => {
349348
this._serverProcess = null;
350349
this._setState(ServerState.Stopped);
351350
this._fireEvent(Events.ServerStop, this);
352-
return;
353351
});
354352
}
355353

0 commit comments

Comments
 (0)