---
# System prepended metadata

title: Constraint for Daemon

---

# Constraint for Daemon


## Don't close anonfd before replying copen

Refer to [constraint for failover](https://hackmd.io/YNsTQqLcQYOZ4gAlFWrNcA?view#constraint-for-failover)


## Don't close anonfd when executing daemon_read() and before replying CACHEFILES_IOC_READ_COMPLETE


1. Don't close anonfd when executing daemon_read()

```
P1                                P2
------------                      --------------
cachefiles_ondemand_daemon_read   cachefiles_ondemand_fd_release
  xa_lock
  # pick one request
  xa_unlock
                                    xa_lock
                                    object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED, i.e. -1
                                    xa_unlock
                                    
  msg->object_id = object->ondemand_id (oops, invalid ondemand_id,i.e. -1)
```


2. Don't close anonfd before replying CACHEFILES_IOC_READ_COMPLETE

When daemon finishes cachefiles_ondemand_daemon_read() and returns to user space, don't close anonfd before CACHEFILES_IOC_READ_COMPLETE is replied.

```
P1                                P2
------------                      --------------
cachefiles_ondemand_daemon_read 
  xa_lock
  # pick one request, e.g. READ request
  xa_unlock
  msg->object_id = object->ondemand_id
 
# return to user space

                                  cachefiles_ondemand_fd_release
                                    # free ondemand_id
                                    
                                  # later ondemand_id is reused for another new object

# start to handle this READ request
  oops, msg->object_id now is reused for another new object
```