@@ -403,11 +403,19 @@ export class Server<
403403Did you try connecting directly to this Durable Object? Try using getServerByName(namespace, id) instead.` ) ;
404404 }
405405 await this . setName ( room ) ;
406- } else if ( this . #status !== "started" ) {
407- // Name was set by a previous request but initialization failed.
408- // Retry initialization so the server can recover from transient
409- // onStart failures.
410- await this . #initialize( ) ;
406+ } else {
407+ const room = request . headers . get ( "x-partykit-room" ) ;
408+ if ( room && room !== this . #_name) {
409+ throw new Error (
410+ `Room name mismatch: this server is "${ this . #_name} " but request has room "${ room } "`
411+ ) ;
412+ }
413+ if ( this . #status !== "started" ) {
414+ // Name was set by a previous request but initialization failed.
415+ // Retry initialization so the server can recover from transient
416+ // onStart failures.
417+ await this . #initialize( ) ;
418+ }
411419 }
412420 const url = new URL ( request . url ) ;
413421
@@ -559,6 +567,11 @@ Did you try connecting directly to this Durable Object? Try using getServerByNam
559567 async #initialize( ) : Promise < void > {
560568 let error : unknown ;
561569 await this . ctx . blockConcurrencyWhile ( async ( ) => {
570+ if ( ! this . #_name) {
571+ const stored =
572+ await this . ctx . storage . get < string > ( "__partyserver_name" ) ;
573+ if ( stored ) this . #_name = stored ;
574+ }
562575 this . #status = "starting" ;
563576 try {
564577 await this . onStart ( this . #_props) ;
@@ -627,9 +640,6 @@ Did you try connecting directly to this Durable Object? Try using getServerByNam
627640 return this . #_name;
628641 }
629642
630- // We won't have an await inside this function
631- // but it will be called remotely,
632- // so we need to mark it as async
633643 async setName ( name : string ) {
634644 if ( ! name ) {
635645 throw new Error ( "A name is required." ) ;
@@ -640,6 +650,7 @@ Did you try connecting directly to this Durable Object? Try using getServerByNam
640650 ) ;
641651 }
642652 this . #_name = name ;
653+ await this . ctx . storage . put ( "__partyserver_name" , name ) ;
643654
644655 if ( this . #status !== "started" ) {
645656 await this . #initialize( ) ;
0 commit comments