Skip to content

Fix Service advertisement race conditions #919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 14, 2025

Conversation

noah-wardlow
Copy link
Contributor

@noah-wardlow noah-wardlow commented Aug 12, 2025

Public API Changes

  • Service.advertise() now returns a
    Promise that resolves when the
    advertisement is complete
  • Service.advertiseAsync() now returns a
    Promise that resolves when the
    advertisement is complete
  • Service.unadvertise() now returns a
    Promise that resolves when the
    unadvertisement is complete
  • Service errors are now emitted as
    'error' events instead of being logged to
    console

Description

  • Implement operation queue: All advertise/unadvertise
    operations are now serialized through a promise queue
    to prevent race conditions
  • Allow safe re-advertisement: advertise() and
    advertiseAsync() automatically unadvertise before
    re-advertising when a service is already advertised
  • Make operations async: Both methods now return
    promises that resolve when the operation completes
  • Prevent double operations: Added internal state
    tracking to prevent duplicate unadvertise operations
  • Fix cleanup order: Event callbacks are removed before
    sending unadvertise messages to stop processing new
    requests immediately

- Implement operation queue: All advertise/unadvertise
  operations are now serialized through a promise queue
  to prevent race conditions
  - Allow safe re-advertisement: advertise() and
  advertiseAsync() automatically unadvertise before
  re-advertising when a service is already advertised
  - Make operations async: Both methods now return
  promises that resolve when the operation completes
  - Prevent double operations: Added internal state
  tracking to prevent duplicate unadvertise operations
  - Fix cleanup order: Event callbacks are removed before
   sending unadvertise messages to stop processing new
  requests immediately
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request fixes race conditions in Service advertisement operations by implementing a serialized operation queue and automatic re-advertisement handling. The changes ensure that advertise and unadvertise operations can be called safely in any order without errors.

  • Implements a promise-based operation queue to serialize all advertise/unadvertise operations
  • Allows automatic re-advertisement by unadvertising before re-advertising when a service is already advertised
  • Converts operations to return promises and prevents duplicate unadvertise operations

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/core/Service.js Implements operation queue, async operation handling, and automatic re-advertisement logic
test/service.test.js Adds comprehensive tests for re-advertisement, multiple unadvertise calls, and operation serialization

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

});
this.isAdvertised = true;
}).catch(err => {
console.error(`Error advertising service ${this.name}:`, err);
Copy link
Preview

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message uses console.error which may not be appropriate for a library. Consider using the EventEmitter's error event or allowing the error to propagate to the caller instead of logging it.

Suggested change
console.error(`Error advertising service ${this.name}:`, err);
this.emit('error', err);

Copilot uses AI. Check for mistakes.

this._operationQueue = this._operationQueue.then(async () => {
await this._doUnadvertise();
}).catch(err => {
console.error(`Error unadvertising service ${this.name}:`, err);
Copy link
Preview

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message uses console.error which may not be appropriate for a library. Consider using the EventEmitter's error event or allowing the error to propagate to the caller instead of logging it.

Suggested change
console.error(`Error unadvertising service ${this.name}:`, err);
this.emit('error', err);

Copilot uses AI. Check for mistakes.

});
this.isAdvertised = true;
}).catch(err => {
console.error(`Error advertising async service ${this.name}:`, err);
Copy link
Preview

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message uses console.error which may not be appropriate for a library. Consider using the EventEmitter's error event or allowing the error to propagate to the caller instead of logging it.

Suggested change
console.error(`Error advertising async service ${this.name}:`, err);
this.emit('error', err);

Copilot uses AI. Check for mistakes.

Copy link
Contributor

@EzraBrooks EzraBrooks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code LGTM - waiting for confirmation that it fixes the observed bug(s) in MoveIt Pro :)

Copy link
Contributor

@EzraBrooks EzraBrooks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appears to solve the issues we're seeing with using services in component lifecycle in React!

@EzraBrooks EzraBrooks merged commit 7311901 into RobotWebTools:develop Aug 14, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants