wechaty / puppet-padlocal

Puppet PadLocal is a Pad Protocol for WeChat
https://wechaty.js.org/docs/puppet-providers/padlocal
Apache License 2.0
642 stars 88 forks source link

Deadlock: `this.state.ready('on')` #116

Closed huan closed 2 years ago

huan commented 2 years ago

The Problem

await puppetPadlocal.start() will never resolve.

Walk through

When we are starting the PuppetPadlocal by calling it's start() method, it will check the this.state.on(): if it's true, then it will wait it becomes ready() then return. This design is to protect the puppet from being called start() on an already starting one.

However, when we are calling the start() the second time, the PuppetPadlocal will run into a deadlock, until the user login. If there's no user login (scan the QR code), then the start() will never resolve.

The below code will wait the this.state.ready():

https://github.com/padlocal/wechaty-puppet-padlocal/blob/a4b04b723fc4f75d08596a2952e08f4b5037983c/src/puppet-padlocal.ts#L145-L148

However, the below code shows that the state will be only ready('on') after the user logged in:

https://github.com/padlocal/wechaty-puppet-padlocal/blob/a4b04b723fc4f75d08596a2952e08f4b5037983c/src/puppet-padlocal.ts#L247

Need to be fixed.

Solution

The best solution is to follow the latest Wechaty v1.x design: use the ServiceCtl and put all start code inside the onStart() method.

The ServiceCtl will take care of all the state related management staffs.

padlocal commented 2 years ago

Fixed on esm branch, publised with 1.11.x.

huan commented 2 years ago

@padlocal It's great to know that this issue has been fixed.

However, could you please link the related commit to this issue so that we can confirm it?

image

And I checked the NPM but can not find the published v1.11 version as you said, could you please help us to double confirm it?

Thank you very much!

Link to:

huan commented 2 years ago

ping @padlocal

padlocal commented 2 years ago

Related commit: https://github.com/wechaty/puppet-padlocal/commit/83aa33176f8295807395d57cd35df0619390d682#diff-2cccb5c313d009e3ce929329f4beaf0e436d38282d01bd2bac421155e200d16aL144