@@ -203,6 +203,8 @@ export async function createDevServer<
203203 let closingPromise : Promise < void > | undefined ;
204204 let unregisterRestart : ( ( ) => void ) | undefined ;
205205
206+ // Keep the restart watcher active when closing server resources,
207+ // so failed restarts can be retried.
206208 const closeServerResources = ( ) => {
207209 if ( ! closingPromise ) {
208210 unregisterRestart ?.( ) ;
@@ -217,11 +219,28 @@ export async function createDevServer<
217219 return closingPromise ;
218220 } ;
219221
222+ // Fully close the server and its restart watcher.
220223 const closeServer = async ( ) => {
221224 await state . restartWatcher ?. close ( ) ;
222225 await closeServerResources ( ) ;
223226 } ;
224227
228+ // Request a manual restart and close the old watcher only after it succeeds.
229+ const restartServer = async ( ) => {
230+ const restarted = await requestRestart ( {
231+ action : 'dev' ,
232+ clear : false ,
233+ logger,
234+ restartManager : context . restartManager ,
235+ } ) ;
236+
237+ if ( restarted ) {
238+ await state . restartWatcher ?. close ( ) ;
239+ }
240+
241+ return restarted ;
242+ } ;
243+
225244 if ( ! middlewareMode ) {
226245 registerCleanup ( closeServer ) ;
227246 }
@@ -237,15 +256,7 @@ export async function createDevServer<
237256 openPage,
238257 closeServer,
239258 printUrls,
240- restartServer : context . restartManager . canRestart
241- ? ( ) =>
242- requestRestart ( {
243- action : 'dev' ,
244- clear : false ,
245- logger,
246- restartManager : context . restartManager ,
247- } )
248- : undefined ,
259+ restartServer : context . restartManager . canRestart ? restartServer : undefined ,
249260 help : shortcutsOptions . help ,
250261 customShortcuts : shortcutsOptions . custom ,
251262 logger,
@@ -440,7 +451,6 @@ export async function createDevServer<
440451 // start watching
441452 state . buildManager ?. watch ( ) ;
442453
443- // Only close server resources before restart; keep the watcher alive for retries.
444454 unregisterRestart = context . restartManager . registerCleanup ( closeServerResources ) ;
445455 state . restartWatcher = watchFilesForRestart ( {
446456 watchFiles : config . dev . watchFiles ,
0 commit comments