@@ -274,55 +274,69 @@ export class Remote {
274274// We can ignore this, it's probably blank!
275275}
276276} )
277+ let remotePID = 0
277278// Store the running port for the current commit in a file for reconnection!
278- const portFilePath = `/tmp/.vscode-remote-${ this . vscodeCommit } -port`
279+ const portFilePath = ( pid = remotePID ) => {
280+ return `/tmp/.vscode-remote-${ pid } `
281+ }
282+ const filtered = running . filter ( ( instance ) => instance . commit === this . vscodeCommit )
283+ if ( filtered . length ) {
284+ remotePID = filtered [ 0 ] . process_id
285+ await this . ipc . execute ( shell , `cat${ portFilePath ( remotePID ) } ` , ( data ) => {
286+ if ( data . trim ( ) ) {
287+ remotePort = Number . parseInt ( data . trim ( ) )
288+ }
289+ } )
290+ }
291+
279292let remotePort = 0
280293if ( running . filter ( ( instance ) => instance . commit === this . vscodeCommit ) ) {
281294await this . ipc . execute ( shell , `cat${ portFilePath } ` , ( data ) => {
282295if ( data . trim ( ) ) {
283296remotePort = Number . parseInt ( data . trim ( ) )
284297}
285298} )
286-
287- this . output . appendLine ( "Found existing server running on port: " + remotePort )
299+ if ( remotePort ) {
300+ this . output . appendLine ( "Found existing server running on port: " + remotePort )
301+ }
288302}
289303
290304if ( ! remotePort ) {
291305remotePort = await new Promise < number > ( ( resolve , reject ) => {
292- const script =
293- binPath +
294- " serve-local --start-server --port 0 --without-connection-token --commit-id " +
295- this . vscodeCommit +
296- " --accept-server-license-terms"
306+ const script = `
307+ ${ binPath } serve-local --start-server --port 0 --without-connection-token --commit-id${ this . vscodeCommit } --accept-server-license-terms &
308+ echo "PID: $!"
309+ wait
310+ `
311+
297312this . ipc
298313?. execute ( shell , script , ( data ) => {
299314const lines = data . split ( "\n" )
300315lines . forEach ( ( line ) => {
301316this . output . appendLine ( line )
302- if ( ! line . startsWith ( "Server bound to" ) ) {
303- return
304- }
305- const parts = line . split ( " " ) . filter ( ( part ) => part . startsWith ( "127.0.0.1:" ) )
306- if ( parts . length === 0 ) {
307- return reject ( "No port found in output: " + line )
317+ if ( line . startsWith ( "PID: " ) ) {
318+ console . log ( "WE GOT PID" , line )
319+ remotePID = Number . parseInt ( line . split ( "PID: " ) [ 1 ] . trim ( ) )
308320}
309- const port = parts [ 0 ] . split ( ":" ) . pop ( )
310- if ( ! port ) {
311- return reject ( "No port found in parts: " + parts . join ( "," ) )
321+ if ( line . startsWith ( "Server bound to" ) ) {
322+ const parts = line . split ( " " ) . filter ( ( part ) => part . startsWith ( "127.0.0.1:" ) )
323+ if ( parts . length === 0 ) {
324+ return reject ( "No port found in output: " + line )
325+ }
326+ const port = parts [ 0 ] . split ( ":" ) . pop ( )
327+ if ( ! port ) {
328+ return reject ( "No port found in parts: " + parts . join ( "," ) )
329+ }
330+ resolve ( Number . parseInt ( port ) )
312331}
313- resolve ( Number . parseInt ( port ) )
314332} )
315333} )
316334. then ( ( exitCode ) => {
317335reject ( "Exited with: " + exitCode )
318336} )
319337} )
320338
321- await this . ipc . execute (
322- shell ,
323- `echo${ remotePort } > /tmp/.vscode-remote-${ this . vscodeCommit } -port` ,
324- ( ) => undefined ,
325- )
339+ await this . ipc . execute ( shell , `echo${ remotePort } >${ portFilePath ( remotePID ) } ` , ( ) => undefined )
326340}
327341
328342const forwarded = await this . ipc . portForward ( remotePort )