Commit Graph

431 Commits

Author SHA1 Message Date
Daniel D’Aquino
20dc672dbf Add sync mechanism to prevent background crashes and fix ndb reopen order
This adds a sync mechanism in Ndb.swift to coordinate certain usage of
nostrdb.c calls and the need to close nostrdb due to app lifecycle
requirements. Furthermore, it fixes the order of operations when
re-opening NostrDB, to avoid race conditions where a query uses an older
Ndb generation.

This sync mechanism allows multiple queries to happen simultaneously
(from the Swift-side), while preventing ndb from simultaneously closing
during such usages. It also does that while keeping the Ndb interface
sync and nonisolated, which keeps the API easy to use from
Swift/SwiftUI and allows for parallel operations to occur.

If Swift Actors were to be used (e.g. creating an NdbActor), the Ndb.swift
interface would change in such a way that it would propagate the need for
several changes throughout the codebase, including loading logic in
some ViewModels. Furthermore, it would likely decrease performance by
forcing Ndb.swift operations to run sequentially when they could run in
parallel.

Changelog-Fixed: Fixed crashes that happened when the app went into background mode
Closes: https://github.com/damus-io/damus/issues/3245
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-12-29 11:01:23 -08:00
alltheseas
5058fb33d7 Restore translated note rendering
Changelog-Fixed: Fixed broken automatic translations
Closes: https://github.com/damus-io/damus/issues/3406
Signed-off-by: alltheseas <64376233+alltheseas@users.noreply.github.com>
2025-12-15 18:15:34 -08:00
Daniel D’Aquino
f844ed9931 Redesign Ndb.swift interface with build safety
This commit redesigns the Ndb.swift interface with a focus on build-time
safety against crashes.

It removes the external usage of NdbTxn and SafeNdbTxn, restricting it
to be used only in NostrDB internal code.

This prevents dangerous and crash prone usages throughout the app, such
as holding transactions in a variable in an async function (which can
cause thread-based reference counting to incorrectly deinit inherited
transactions in use by separate callers), as well as holding unsafe
unowned values longer than the lifetime of their corresponding
transactions.

Closes: https://github.com/damus-io/damus/issues/3364
Changelog-Fixed: Fixed several crashes throughout the app
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-12-07 11:02:45 -08:00
Daniel D’Aquino
b562b930cc Prevent new NostrDB streaming tasks from opening when NostrDB has begun to close
We have mechanisms in place to close NostrDB streams when the database needs to
close; however, there is a short time window where those streams are
closing down but the database still has its "open" property set to `true`,
which means that new NostrDB streams may open. If that happens, those
new streams will still be active when NostrDB gets closed down,
potentially causing memory crashes.

This was found by inspecting several crash logs and noticing that:
- most of the `ndb.close` calls are coming from the general
  backgrounding task (not the last resort backgrounding task),
  where all old tasks are guaranteed to have closed (we wait for all of
  them to close before proceeding to closing NostrDB).
- the stack traces of the crashed threads show that, in most cases, the
  stream crashes while they are in the query stage (which means that
  those must have been very recently opened).

The issue was mitigated by signalling that NostrDB has closed (without
actually closing it) before cancelling any streaming tasks and officially
closing NostrDB. This way, new NostrDB streaming tasks will notice that
the database is closed and will wait for it to reopen.

No changelog entry is needed as this issue was introduced after our last public
release.

Changelog-None
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-12-07 11:02:45 -08:00
Daniel D’Aquino
2f7a40bd50 Remove typed throws in some Ndb functions
Those are unused and it causes awkward implementations when different
error types need to be used.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-12-07 11:02:45 -08:00
Daniel D’Aquino
52115d07c2 Fix profile crash
This fixes a crash that would occasionally occur when visiting profiles.

NdbTxn objects were being deinitialized on different threads from their
initialization, causing incorrect reference count decrements in thread-local
transaction dictionaries. This led to premature destruction of shared ndb_txn
C objects still in use by other tasks, resulting in use-after-free crashes.

The root cause is that Swift does not guarantee tasks resume on the same
thread after await suspension points, while NdbTxn's init/deinit rely on
thread-local storage to track inherited transaction reference counts.

This means that `NdbTxn` objects cannot be used in async functions, as
that may cause the garbage collector to deinitialize `NdbTxn` at the end
of such function, which may be running on a different thread at that
point, causing the issue explained above.

The fix in this case is to eliminate the `async` version of the
`NdbNoteLender.borrow` method, and update usages to utilize other
available methods.

Note: This is a rewrite of the fix in https://github.com/damus-io/damus/pull/3329

Note 2: This relates to the fix of an unreleased feature, so therefore no
changelog is needed.

Changelog-None
Co-authored-by: alltheseas <64376233+alltheseas@users.noreply.github.com>
Closes: https://github.com/damus-io/damus/issues/3327
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-11-21 14:59:00 -08:00
ericholguin
b8c664d354 Damus Live
This PR adds Live Streaming and Live Chat to Damus via Damus Labs.

Changelog-Added: Added live stream timeline
Changelog-Added: Added live chat timeline
Changelog-Added: Added ability to create live chat event
Changelog-Added: Damus Labs Toggle

Signed-off-by: ericholguin <ericholguin@apache.org>
2025-11-14 15:36:43 -08:00
Daniel D’Aquino
58e6a49bcf Fix race condition leading to intermittent issues with ndb streaming and related tests
A race condition was identified where notes would get dropped if they
get indexed in the time window between when a query is made and the subscription is made.

The issue was fixed by making the subscribe call before making the query
call, to ensure we get all notes from that time when we perform the
query.

This dropped the failure rate for ndb subscription tests from about 20%
down to about 4%.

Local relay model issue was not publicly released, which is why the
changelog entry is "none".

Changelog-None
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-24 18:40:32 -07:00
Daniel D’Aquino
7afcaa99fe Reduce race condition probability in Ndb streaming functions
This attempts to reduce race conditions coming from Ndb streaming
functions that could lead to lost notes or crashes.

It does so by making two improvements:
1. Instead of callbacks, now the callback handler uses async streams,
   which reduces the chances of a callback being called before the last
   item was processed by the consumer.
2. The callback handler will now queue up received notes if there are
   no listeners yet. This is helpful because we need to issue the
   subscribe call to nostrdb before getting the subscription id and
   setting up a listener, but in between that time nostrdb may still
   send notes which would effectively get dropped without this queuing
   mechanism.

Changelog-Fixed: Improved robustness in the part of the code that streams notes from nostrdb
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-24 16:25:44 -07:00
Daniel D’Aquino
991a4a86e6 Move most of RelayPool away from the Main Thread
This is a large refactor that aims to improve performance by offloading
RelayPool computations into a separate actor outside the main thread.

This should reduce congestion on the main thread and thus improve UI
performance.

Also, the internal subscription callback mechanism was changed to use
AsyncStreams to prevent race conditions newly found in that area of the
code.

Changelog-Fixed: Added performance improvements to timeline scrolling
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-10 16:38:48 -07:00
Daniel D’Aquino
7691b48fb6 Fix testDecodingPayInvoiceRequest test failure
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-06 11:48:58 -07:00
Daniel D’Aquino
3437cf5347 Further improvements to app lifecycle handling
- Resend subscription requests to relays when websocket connection is
  re-established
- More safeguard checks on whether Ndb is opened before accessing its
  memory
- Cancel queued unsubscribe requests on app backgrounding to avoid race
  conditions with subscribe requests when app enters the foreground
- Call Ndb re-open when Damus is active (not only on active notify), as
  experimentally there have been instances where active notify code has
  not been run. The operation is idempotent, so there should be no risk
  of it being called twice.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-10-05 13:18:59 -07:00
Daniel D’Aquino
258d08723f Check if Ndb is closed before running subscribe and query operations
This should prevent background crashes caused by race conditions between
usages of Ndb and the Ndb/app lifecycle operations.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-26 12:04:51 -07:00
Daniel D’Aquino
a09e22df24 Improve streaming interfaces and profile loading logic
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:11 -07:00
Daniel D’Aquino
a3ef36120e Fix OS 26 build errors
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:10 -07:00
Daniel D’Aquino
2185984ed7 Stream from both NDB and network relays
This commit takes a step back from the full local relay model by
treating NostrDB as one of the many relays streamed from, instead of the
one exclusive relay that other classes rely on.

This was done to reduce regression risk from the local relay model
migration, without discarding the migration work already done.

The full "local relay model" behavior (exclusive NDB streaming) was
hidden behind a feature flag for easy migration later on.

Closes: https://github.com/damus-io/damus/issues/3225
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:10 -07:00
Daniel D’Aquino
1caad24364 Add note provenance filter support to SubscriptionManager
Closes: https://github.com/damus-io/damus/issues/3222
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:10 -07:00
Daniel D’Aquino
ecbfb3714b Fix incompatibilities with new nostrdb version
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:03 -07:00
William Casarin
d565eb20f7 nostrdb: query: enforce author matches in author_kind queries
before we weren't checking this, meaning we were getting
results from other keys. oops.

Reported-by: Jeff Gardner
Fixes: #84
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
a040a0244b nostrdb: search: fix memleak in profile search
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
387af198d6 nostrdb: win: fix heap corruption with flatbuf 2025-09-24 14:06:03 -07:00
William Casarin
66e10db6b2 nostrdb: mem: re-enable profile freeing
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
42a0f2c08d nostrdb: Revert "mem: search cursor close"
this is causing heap corruption on the windows
build

This reverts commit a8d6925a5b33ddbdd4306423527b5d8314f7dd36.
2025-09-24 14:06:03 -07:00
William Casarin
aa8ce31941 nostrdb: mem: close cursors in print helpers 2025-09-24 14:06:03 -07:00
William Casarin
8014d772ba nostrdb: mem: builder clear before free 2025-09-24 14:06:03 -07:00
William Casarin
4d8313c788 nostrdb: mem: relay iter cleanup 2025-09-24 14:06:03 -07:00
William Casarin
342067640f nostrdb: mem: reaction stats cleanup 2025-09-24 14:06:03 -07:00
William Casarin
84839d1c43 nostrdb: mem: search cursor close 2025-09-24 14:06:03 -07:00
William Casarin
b5079c42d5 nostrdb: memory: fix a bunch of memory leaks
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
0847c53a39 nostrdb: add ndb_builder_push_tag_id
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
fa2d240ddf nostrdb: nostrdb: calculate id in ndb_note_verify
Rogue relays could in theory attack nostrdb by replaying ids and
signatures from other notes. This fixes this weakness by calculating the
id again in ndb_note_verify.

There is no known relays exploiting this, but lets get ahead of it
before we switch to the outbox model in damus iOS/notedeck

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
3a37a6c18e nostrdb: change <=10 author search queries to ==1
These queries are broken anyways. Rely on scans until we fix this

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
5c75e87ed5 nostrdb: eq: fix fallthrough bug
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
64c16e7cc8 nostrdb: filter: add initial custom filtering logic
This adds some helpers for adding custom filtering logic
to nostr filters. These are just a callback and a closure.
There can only be one custom callback filter per filter.

Fixes: https://github.com/damus-io/nostrdb/issues/33
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
0b8090cb28 nostrdb: query: implement profile search query plans
The basic idea of this is to allow you to use the standard
nip50 query interface to search for profiles using our profile
index.

query: {"search":"jb55", "kinds":[0]}

will result in a profile_search query plan that searches kind0 profiles
for the corresponding `name` or `display_name`.

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
9cff8608f6 nostrdb: fix build on macos 2025-09-24 14:06:03 -07:00
William Casarin
c728210be8 nostrdb: query: implement author_kind query plan
This should help author kind query performance
2025-09-24 14:06:03 -07:00
William Casarin
0f66e87faf nostrdb: Relay queries
Add support for relay-based filtering in nostr queries.

Filters can now include a "relays" field. Optimal performance when
you include a kind as well:

{"relays":["wss://pyramid.fiatjaf.com/"], "kinds":[1]}

This corresponds to a `ndb` query like so:

$ ndb query -r wss://pyramid.fiatjaf.com/ -k 1 -l 1
using filter '{"relays":["wss://pyramid.fiatjaf.com/"],"kinds":[1],"limit":1}'
1 results in 0.094929 ms
{"id":"277dd4ed26d0b44576..}

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
af2298dcb7 nostrdb: relay: fix potential relay index corruption
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
a0b85129d4 nostrdb: relay: fix race condition bug
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
e42b14cc6f nostrdb: debug: add a print for debugging rust integration
Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
f0521ba406 nostrdb: relay-index: fix a few bugs
There were a few race conditions and lmdb bugs in the
relay index implementation. Fix those!

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
c29027ff5b nostrdb: note: always write relay index
This fixes a race condition where if multiple of the same note
is processed at the same time, we still manage to write the
note relays

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
c6674199de nostrdb: win: fix build on windows 2025-09-24 14:06:03 -07:00
William Casarin
5961bf7958 nostrdb: ndb: add print-relay-kind-index-keys
for debugging

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
a877a19c25 nostrdb: relay: add note relay iteration
This is a simple cursor that walks the NDB_DB_NOTE_RELAYS db

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
684701931d nostrdb: Initial relay index implementation
Add relay indexing for existing notes

This patch introduces a relay index for new notes and notes that have
already been stored, allowing the database to track additional relay
sources for a given note.

Changes:

- Added `NDB_WRITER_NOTE_RELAY` to handle relay indexing separately from
  new note ingestion.

- Implemented `ndb_write_note_relay()` and
  `ndb_write_note_relay_kind_index()` to store relay URLs.

- Modified `ndb_ingester_process_event()` to check for existing notes
  and append relay info if necessary.

- Introduced `ndb_note_has_relay()` to prevent duplicate relay entries.

- Updated LMDB schema with `NDB_DB_NOTE_RELAYS` (note_id -> relay) and
  `NDB_DB_NOTE_RELAY_KIND` (relay + kind + created_at -> note).

- Refactored `ndb_process_event()` to use `ndb_ingest_meta` for tracking
  relay sources.

- Ensured proper memory management for relay strings in writer thread.

With this change, nostrdb can better track where notes are seen across
different relays, improving query capabilities for relay-based data
retrieval.

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
William Casarin
fcd8131063 nostrdb: config: custom writer scratch size
making more things configurable if you have memory constraints

Signed-off-by: William Casarin <jb55@jb55.com>
2025-09-24 14:06:03 -07:00
Daniel D’Aquino
3290e1f9d2 Improve NostrNetworkManager interfaces
This commit improves NostrNetworkManager interfaces to be easier to use,
and with more options on how to read data from the Nostr network

This reduces the amount of duplicate logic in handling streams, and also
prevents possible common mistakes when using the standard subscribe method.

This fixes an issue with the mute list manager (which prompted for this
interface improvement, as the root cause is similar to other similar
issues).

Closes: https://github.com/damus-io/damus/issues/3221
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:02 -07:00
Daniel D’Aquino
c4c3656f90 Multi-session subscriptions and RelayPool reopening
This commit implements nostr network subscriptions that survive between
sessions, as well as improved handling of RelayPool opening/closing with
respect to the app lifecycle.

This prevents stale data after users swap out and back into Damus.

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
2025-09-24 14:06:02 -07:00