Skip to content
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

flash() callback triggered before connection.serialPort is confirmed closed #254

Open
cerilloderek opened this issue Mar 10, 2022 · 0 comments

Comments

@cerilloderek
Copy link

cerilloderek commented Mar 10, 2022

Description

When passing a port to the Avrgirl class, ideally, that port should be ready for use once the callback passed to avrgirl.flash() is triggered. That allows clients to immediately use the port after a flash without adding some arbitrary wait time.

Expected behaviour

The port passed to the Avrgirl constructor is free and available for other services to connect to once avrgirl.flash() has finished.

Actual behaviour

avrgirl-arduino instantiates a new port connection.serialPort which can sometimes still be in use by the time the callback in flash() has been entered. This prevented accessing that port immediately.

Operating system and version

Win 10 - November 2021 Update
Could not reproduce on Ubuntu 20.04.4 LTS (Focal Fossa)

Avrgirl Arduino version

5.0.1

NodeJS version

14.18.3

Arduino Board being used

ATmega1284p

Log output, if available

Step by step guide to reproducing the issue

await new Promise<boolean>((resolve, reject) => {
  this._avrgirl.flash(path, (err: Error) => {
    if (err) {
      reject(err);
    } else {
      resolve(true);
    }
  });
});
const p = new SerialPort(<port path>);
p.open((err) => {
  // results in ACCESS DENIED error because the `avrgirl.connection.serialPort` is still open
});

Workaround - wait for connection.serialPort to close before trying to access the port

  public async flash(path: string): Promise<boolean> {
    const flashComplete = new Promise<boolean>((resolve, reject) => {
      this._avrgirl.flash(path, (err: Error) => {
        if (err) {
          reject(err);
        } else {
          resolve(true);
        }
      });
    });

    // Need to ensure the device that AVR girl created is closed
    const portClosed = new Promise<boolean>((resolve, reject) => {
      this._avrgirl.connection.serialPort.on('close', (err) => {
        if (err) {
          reject(err);
        } else {
          resolve(true);
        }
      });
    });

    const deviceReady = await Promise.all([portClosed, flashComplete]);
    return deviceReady[0] && deviceReady[1];
 }
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

No branches or pull requests

1 participant