@@ -229,28 +229,46 @@ export class PCTransportManager {
229229
230230 async negotiate ( abortController : AbortController ) {
231231 return new TypedPromise < void , NegotiationError | Error > ( async ( resolve , reject ) => {
232- const negotiationTimeout = setTimeout ( ( ) => {
232+ let negotiationTimeout = setTimeout ( ( ) => {
233233 reject ( new NegotiationError ( 'negotiation timed out' ) ) ;
234234 } , this . peerConnectionTimeout ) ;
235235
236- const abortHandler = ( ) => {
236+ const cleanup = ( ) => {
237237 clearTimeout ( negotiationTimeout ) ;
238+ this . publisher . off ( PCEvents . NegotiationStarted , onNegotiationStarted ) ;
239+ abortController . signal . removeEventListener ( 'abort' , abortHandler ) ;
240+ } ;
241+
242+ const abortHandler = ( ) => {
243+ cleanup ( ) ;
238244 reject ( new NegotiationError ( 'negotiation aborted' ) ) ;
239245 } ;
240246
241- abortController . signal . addEventListener ( 'abort' , abortHandler ) ;
242- this . publisher . once ( PCEvents . NegotiationStarted , ( ) => {
247+ // Reset the timeout each time a renegotiation cycle starts. This
248+ // prevents premature timeouts when the negotiation machinery is
249+ // actively renegotiating (offers going out, answers coming back) but
250+ // NegotiationComplete hasn't fired yet because new requirements keep
251+ // arriving between offer/answer round-trips.
252+ const onNegotiationStarted = ( ) => {
243253 if ( abortController . signal . aborted ) {
244254 return ;
245255 }
246- this . publisher . once ( PCEvents . NegotiationComplete , ( ) => {
247- clearTimeout ( negotiationTimeout ) ;
248- resolve ( ) ;
249- } ) ;
256+ clearTimeout ( negotiationTimeout ) ;
257+ negotiationTimeout = setTimeout ( ( ) => {
258+ cleanup ( ) ;
259+ reject ( new NegotiationError ( 'negotiation timed out' ) ) ;
260+ } , this . peerConnectionTimeout ) ;
261+ } ;
262+
263+ abortController . signal . addEventListener ( 'abort' , abortHandler ) ;
264+ this . publisher . on ( PCEvents . NegotiationStarted , onNegotiationStarted ) ;
265+ this . publisher . once ( PCEvents . NegotiationComplete , ( ) => {
266+ cleanup ( ) ;
267+ resolve ( ) ;
250268 } ) ;
251269
252270 await this . publisher . negotiate ( ( e ) => {
253- clearTimeout ( negotiationTimeout ) ;
271+ cleanup ( ) ;
254272 if ( e instanceof Error ) {
255273 reject ( e ) ;
256274 } else {
0 commit comments