77 */
88
99import { getHomeDir , runPIOCommand } from './core' ;
10+ import { reportError , sleep } from './misc' ;
1011
1112import SockJS from 'sockjs-client' ;
1213import fs from 'fs-plus' ;
1314import jsonrpc from 'jsonrpc-lite' ;
1415import path from 'path' ;
1516import qs from 'querystringify' ;
16- import { reportError } from './misc' ;
1717import request from 'request' ;
1818import semver from 'semver' ;
1919import tcpPortUsed from 'tcp-port-used' ;
@@ -22,6 +22,8 @@ import ws from 'ws';
2222
2323const SERVER_LAUNCH_TIMEOUT = 5 * 60 ; // 5 minutes
2424const HTTP_HOST = '127.0.0.1' ;
25+ const HTTP_PORT_BEGIN = 8010 ;
26+ const HTTP_PORT_END = 8100 ;
2527let HTTP_PORT = 0 ;
2628let IDECMDS_LISTENER_STATUS = 0 ;
2729
@@ -103,9 +105,9 @@ async function listenIDECommands(callback) {
103105}
104106
105107async function findFreePort ( ) {
106- let port = 8010 ;
108+ let port = HTTP_PORT_BEGIN ;
107109 let inUse = false ;
108- while ( port < 9000 ) {
110+ while ( port < HTTP_PORT_END ) {
109111 inUse = await new Promise ( resolve => {
110112 tcpPortUsed . check ( port , HTTP_HOST )
111113 . then ( result => {
@@ -137,13 +139,21 @@ export async function ensureServerStarted(options={}) {
137139 const maxAttempts = 3 ;
138140 let attemptNums = 0 ;
139141 let lastError = undefined ;
142+ let _port = 0 ;
140143 while ( attemptNums < maxAttempts ) {
141144 try {
142145 return await _ensureServerStarted ( options ) ;
143146 } catch ( err ) {
144147 lastError = err ;
145148 console . warn ( err ) ;
146149 HTTP_PORT = 0 ;
150+ // stop all PIO Home servers
151+ _port = HTTP_PORT_BEGIN ;
152+ while ( _port < HTTP_PORT_END ) {
153+ request . get ( `http://${ HTTP_HOST } :${ _port } ?__shutdown__=1` ) . on ( 'error' , ( ) => { } ) ;
154+ _port ++ ;
155+ }
156+ await sleep ( 2000 ) ; // wait for 2 secs while server stops
147157 }
148158 attemptNums ++ ;
149159 }
0 commit comments