appdaemon icon indicating copy to clipboard operation
appdaemon copied to clipboard

run_in(delay=0) from initialize() not executing

Open CZDanol opened this issue 5 months ago • 12 comments

What happened?

Hello, I have found out what seems to be a bug in the scheduler. In the provided code, the "TEST" never gets logged, unless you set the delay to for examplae 0.5

Version

4.5.11

Installation type

Running from my dev env.

Relevant log output

2025-07-27 12:41:34.057813 INFO AppDaemon: Starting apps: {'main'}
2025-07-27 12:41:34.058817 INFO AppDaemon: Calling initialize() for main
2025-07-27 12:41:34.160941 INFO main: XXX
2025-07-27 12:41:34.161441 INFO AppDaemon: App initialization complete

Relevant code in the app or config file that caused the issue

class Main(Hass):
    def initialize(self) -> None:
        self.run_in(lambda _: self.log("TEST"), delay=0)
        self.log("XXX")

Anything else?

No response

CZDanol avatar Jul 27 '25 10:07 CZDanol

I have the same issue. Therefore, I'm always using a delay of 1 where the callback should be scheduled for execution on app start.

There is a similar issue in run_every() where I also described the zero delay issue in run_in(): #2384

I also did some testing:

class Main(Hass):
    async def initialize(self):
        for index in range(10):
            await self.ad_api.run_in(self.test_delay, 0, index=index)

    async def test_delay(self, index, **_):
        date_time = datetime.datetime.now().strftime("%H:%M:%S.%f")
        print(f"test_delay[{index}]: {date_time}")

In case of async (like in my example), most of the print calls are not executed. In my case, only the last one gets executed:

test_delay[9]: 21:32:46.495398

But in case of non-async code (i.e. def instead of async def), all print calls are correctly executed:

test_delay[0]: 21:35:50.356242
test_delay[1]: 21:35:50.357777
test_delay[2]: 21:35:50.359025
test_delay[3]: 21:35:50.360238
test_delay[4]: 21:35:50.361282
test_delay[5]: 21:35:50.362269
test_delay[6]: 21:35:50.363223
test_delay[7]: 21:35:50.364252
test_delay[8]: 21:35:50.365219
test_delay[9]: 21:35:50.366318

But for some reason, that code is working perfectly fine, even if using async:

class Main(Hass):
    async def initialize(self):
        await self.ad_api.run_in(self.test, 1)

    async def test(self, **_):
        for index in range(10):
            await self.ad_api.run_in(self.test_delay, 0, index=index)

    async def test_delay(self, index, **_):
        date_time = datetime.datetime.now().strftime("%H:%M:%S.%f")
        print(f"test_delay[{index}]: {date_time}")

Programie avatar Jul 27 '25 19:07 Programie

Same here. This used to work with older appdaemons. Does not work with 4.5.11

Also, I think it doesn't work with any time too short. (so, delay=.1 works, but delay=0.0000001 does not). Not 100% sure on that.

rr326 avatar Aug 16 '25 16:08 rr326

We have a fix on the way

acockburn avatar Aug 16 '25 16:08 acockburn

@acockburn Any idea when this fix will go to production or when it was introduced? My system isn't working, so I'm trying to figure out if I should 1) keep waiting; 2) revert; 3) create some sort of monkey patch for the interim.

rr326 avatar Sep 05 '25 14:09 rr326

We believe we know why this is happening, and a fix is on the agenda, we are still discussing options as it's a side effect of some other changes that went in.

For now, I think if you change the Rubin to 1s instead of 0s it should work.

acockburn avatar Sep 05 '25 14:09 acockburn

FYI i’m pretty sure that didn’t work for one of my cases. When I was digging into it, I was under the impression that it was a timing issue not simply getting to go to the scheduler at all.

I guess I’ll patch it all for now. Thanks.

-- Sent by phone

On September 5, 2025 at 7:45:09 AM, Andrew Cockburn ( @.***) wrote:

acockburn left a comment (AppDaemon/appdaemon#2405) https://github.com/AppDaemon/appdaemon/issues/2405#issuecomment-3258611576

We believe we know why this is happening, and a fix is on the agenda, we are still discussing options as it's a side effect of some other changes that went in.

For now, I think if you change the Rubin to 1s instead of 0s it should work.

— Reply to this email directly, view it on GitHub https://github.com/AppDaemon/appdaemon/issues/2405#issuecomment-3258611576, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANPH57CEKOZPB27MVGR6JT3RGOXLAVCNFSM6AAAAACCOWFO7KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTENJYGYYTCNJXGY . You are receiving this because you commented.Message ID: @.***>

rr326 avatar Sep 05 '25 14:09 rr326

If it's what we're thinking is, you need enough of a delay to allow initialize() to complete, then you should be good.

acockburn avatar Sep 05 '25 14:09 acockburn

Exactly. That is consistent. I had a slow initialize.

-- Sent by phone

On September 5, 2025 at 7:53:50 AM, Andrew Cockburn ( @.***) wrote:

acockburn left a comment (AppDaemon/appdaemon#2405) https://github.com/AppDaemon/appdaemon/issues/2405#issuecomment-3258637700

If it's what we're thinking is, you need enough of a delay to allow initialize() to complete, then you should be good.

— Reply to this email directly, view it on GitHub https://github.com/AppDaemon/appdaemon/issues/2405#issuecomment-3258637700, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANPH54BGK2O6AQU7ASMPHL3RGPX5AVCNFSM6AAAAACCOWFO7KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTENJYGYZTONZQGA . You are receiving this because you commented.Message ID: @.***>

rr326 avatar Sep 05 '25 15:09 rr326

Good to know - it's up to you though, we're discussing a fix that will be in the next release which should be soon as in a week or two, and everything should just start working again, it's up to you if you want to wait

acockburn avatar Sep 05 '25 15:09 acockburn

A week or two works for me. Thanks soft saving me the effort

-- Sent by phone

On September 5, 2025 at 8:25:49 AM, Andrew Cockburn ( @.***) wrote:

acockburn left a comment (AppDaemon/appdaemon#2405) https://github.com/AppDaemon/appdaemon/issues/2405#issuecomment-3258776179

Good to know - it's up to you though, we're discussing a fix that will be in the next release which should be soon as in a week or two, and everything should just start working again, it's up to you if you want to wait

— Reply to this email directly, view it on GitHub https://github.com/AppDaemon/appdaemon/issues/2405#issuecomment-3258776179, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANPH54IDPZJWMER774JUED3RGTP3AVCNFSM6AAAAACCOWFO7KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTENJYG43TMMJXHE . You are receiving this because you commented.Message ID: @.***>

rr326 avatar Sep 05 '25 15:09 rr326

This should work in dev now

jsl12 avatar Sep 27 '25 18:09 jsl12

@jsl12 How solid is dev? I'd be happy to test out but should I wait til you guys get a release candidate or something? I don't want to upgrade prematurely and brake teh rest of my production system.

rr326 avatar Sep 28 '25 15:09 rr326