# Letting a watch timeout

**URL:** <https://forums.foundationdb.org/t/letting-a-watch-timeout/3730>\
**Category:** Using FoundationDB\
**Created:** [January 9, 2023, 5:47pm UTC](https://forums.foundationdb.org/t/letting-a-watch-timeout/3730 "2023-01-09T17:47:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![slowfranklin](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/slowfranklin/32/1490_2.png) [@slowfranklin](https://forums.foundationdb.org/u/slowfranklin)\
**Post date:** [January 9, 2023, 5:47pm UTC](https://forums.foundationdb.org/t/letting-a-watch-timeout/3730/1 "2023-01-09T17:47:58Z")

</div>

Hi folks!

The following Python script doesn’t trigger the transaction timeout on a pending watch operation. Am I doing something wrong?

```auto
import fdb
fdb.api_version(710)
db = fdb.open()
db.set(fdb.tuple.pack((1,)), fdb.tuple.pack((2,)))
tr = db.create_transaction()
tr.options.set_timeout(1000)
tr.watch(fdb.tuple.pack((1,))).wait()

```

I would expect the transaction to timeout after one second. What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![slowfranklin](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/slowfranklin/32/1490_2.png) [@slowfranklin](https://forums.foundationdb.org/u/slowfranklin)\
**Post date:** [January 9, 2023, 6:25pm UTC](https://forums.foundationdb.org/t/letting-a-watch-timeout/3730/2 "2023-01-09T18:25:11Z")

</div>

OK, guess I got the txn API completely wrong. The following should make more sense, but I still don’t get a timeout:

```auto
import fdb
fdb.api_version(710)
db = fdb.open()
db.set(fdb.tuple.pack((1,)), fdb.tuple.pack((2,)))
tr = db.create_transaction()
tr.options.set_retry_limit(1)
tr.options.set_timeout(1000)
w = tr.watch(fdb.tuple.pack((1,)))
tr.commit()
w.wait()

```

Any suggestion? Thanks a lot!

---

<div class="post-metadata">

**Author:** ![slowfranklin](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/slowfranklin/32/1490_2.png) [@slowfranklin](https://forums.foundationdb.org/u/slowfranklin)\
**Post date:** [January 10, 2023, 1:40pm UTC](https://forums.foundationdb.org/t/letting-a-watch-timeout/3730/3 "2023-01-10T13:40:22Z")

</div>

Wooly thinking. So the lifetime of the watch is of course independent from the txn that created it and there doesn’t seem to be a way to let a watch timeout. So instead I tried a dump version of blocking synchronously on a watch by checking with watch.is\_ready() and sleeping for some short amount of time if not. But now I’m not receiving any watch notifications and the test program just times out:

```auto
import fdb
import time

fdb.api_version(710)
db = fdb.open()
db.set(fdb.tuple.pack(("key",)), fdb.tuple.pack((time.time(),)))
tr = db.create_transaction()
w = tr.watch(fdb.tuple.pack(("key",)))
tr.commit()

start = time.time()
while True:
    if w.is_ready():
        print("value: %s" % w)
        break
    now = time.time()
    if (now - 10 > start):
        print("timeout\n")
        break
    time.sleep(1)

```

I’m running this twice: after starting a first instance I start a second one 1 or 2 seconds later. So the second instance should trigger the watch afaict.

What am I doing wrong this time? 🙂 Thanks!

---

<div class="post-metadata">

**Author:** ![slowfranklin](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/slowfranklin/32/1490_2.png) [@slowfranklin](https://forums.foundationdb.org/u/slowfranklin)\
**Post date:** [January 10, 2023, 2:18pm UTC](https://forums.foundationdb.org/t/letting-a-watch-timeout/3730/4 "2023-01-10T14:18:44Z")

</div>

Woohoo! Actually waiting for the txn to be committed fixes it, so `tr.commit().wait()` instead of just `tr.commit()`
