# App lockfile or supervisor lockfile working too well

**URL:** <https://forums.balena.io/t/app-lockfile-or-supervisor-lockfile-working-too-well/4532>\
**Category:** Product support\
**Created:** [November 27, 2018, 5:27pm UTC](https://forums.balena.io/t/app-lockfile-or-supervisor-lockfile-working-too-well/4532 "2018-11-27T17:27:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [November 27, 2018, 5:27pm UTC](https://forums.balena.io/t/app-lockfile-or-supervisor-lockfile-working-too-well/4532/1 "2018-11-27T17:27:44Z")

</div>

I’m creating my application lockfile using flock:

```
while true; do
    echo "starting camera_controller_resin ${SERVER_URL}"
    flock /tmp/resin/resin-updates.lock -c "./camera_controller_resin \"${SERVER_URL}\""
done

```

And what I’m seeing is that the update is downloaded, and often only applied if I force a reboot through the actions panel. Sometimes I can reset the application and get the update applied, sometimes I can reboot or reset the device but often get the error:

```
Request error: Updates are locked: EEXIST: file already exists, open '/mnt/root/tmp/balena-supervisor/services

```

Devices are running:

- Resin OS 2.13.6+rev2
- balenaOS 2.26.0+rev1

I have tried enabling the lock override, and setting it back to disabled. But that appears to only affect the app locks. The error above appears to be to the balena supervisor, which requires using the device Action Reboot.

Suggested workflow for addressing the root issue?

---

<div class="post-metadata">

**Author:** ![xginn8](https://avatars.discourse-cdn.com/v4/letter/x/8baadc/32.png) [@xginn8](https://forums.balena.io/u/xginn8)\
**Post date:** [November 27, 2018, 11:47pm UTC](https://forums.balena.io/t/app-lockfile-or-supervisor-lockfile-working-too-well/4532/4 "2018-11-27T23:47:42Z")

</div>

Hi there @jason10,

Lockfiles are typically used to prevent the supervisor from killing an application at a critical time, such as preventing data loss. It looks to me like you are daemonizing the application while locked, such that the supervisor cannot ever update it (without the interventions you mention). It stands to reason that the supervisor would refuse to restart an application while the lockfile is held exclusively.

What you are saying is that the reboot actions only work some fraction of the time, and sometimes that `EEXIST` error is thrown. Is that correct? It looks like the line was cut off, is it possible for you to paste the whole line in the logs?

Thank you very much!

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [November 30, 2018, 8:09pm UTC](https://forums.balena.io/t/app-lockfile-or-supervisor-lockfile-working-too-well/4532/6 "2018-11-30T20:09:53Z")

</div>

Hi Matthew,

My program exits occasionally so that the lock can be released. Perhaps adding a `sleep 1` would help.  
Is there a way for my program to check that an update is ready to be applied?

Yes the reboot works sometimes, and other times I get the error.

Here is the error in full:  
`Request error: Updates are locked: EEXIST: file already exists, open '/mnt/root/tmp/resin-supervisor/services/1275531/main/resin-updates.lock'`

So is that because my app has created a lock or because of a different issue?

---

<div class="post-metadata">

**Author:** ![CameronDiver](https://avatars.discourse-cdn.com/v4/letter/c/2acd7d/32.png) [@CameronDiver](https://forums.balena.io/u/CameronDiver)\
**Post date:** [December 6, 2018, 5:38pm UTC](https://forums.balena.io/t/app-lockfile-or-supervisor-lockfile-working-too-well/4532/9 "2018-12-06T17:38:46Z")

</div>

@jason10  
It is possible to detect if a update is ready to apply while the lock has been taken by using the following endpoint on the supervisor api:

```bash
curl --header "Content-type:application/json" "$RESIN_SUPERVISOR_ADDRESS/v2/applications/state?apikey=$RESIN_SUPERVISOR_API_KEY"

```

which will return an object similar to:

```json
{
  "supervisortest": {
    "appId": 1011165,
    "commit": "42a5d01723cac00538fb10df5ea0a671",
    "services": {
      "main": {
        "status": "Downloaded",
        "releaseId": 688486,
        "downloadProgress": null
      }
    }
  }
}

```

What you’re looking for is the `Downloaded` status on the services.  
Let me know if this helps you!

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [December 7, 2018, 9:00pm UTC](https://forums.balena.io/t/app-lockfile-or-supervisor-lockfile-working-too-well/4532/11 "2018-12-07T21:00:16Z")

</div>

@jason10 to add to Cameron’s answer - that EEXIST error on the supervisor is expected, and is indeed just the supervisor’s way of saying it’s not applying the update because your app has taken the lock.

I think indeed adding some sleep time between the locking attempts would help - the supervisor will only attempt to take the lock periodically, so if the window is too small it is likely that it will never be able to take it.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [January 9, 2019, 11:23pm UTC](https://forums.balena.io/t/app-lockfile-or-supervisor-lockfile-working-too-well/4532/13 "2019-01-09T23:23:37Z")

</div>

@pcarranzav thanks, I’ve removed the flock command until we resolve these issues with a solution that actually pays attention to updates.

When the flock command was in place in that tight loop, we essentially had to login to the device and kill the container in order to perform updates, reboot, shutdown etc.

```auto
while true; do
    flock /tmp/resin/resin-updates.lock -c echo "You're not the boss of me!"
done

```
