diff --git a/crates/core/src/sync/storage_adapter.rs b/crates/core/src/sync/storage_adapter.rs index 5f8411d..a1af3d5 100644 --- a/crates/core/src/sync/storage_adapter.rs +++ b/crates/core/src/sync/storage_adapter.rs @@ -333,8 +333,9 @@ WHERE bucket = ?1", pub fn collect_subscription_requests( &self, include_defaults: bool, + now: TimestampMicros, ) -> Result { - self.delete_outdated_subscriptions()?; + self.delete_outdated_subscriptions(now)?; let mut subscriptions: Vec = Vec::new(); let mut index_to_local_id = Vec::::new(); @@ -394,8 +395,7 @@ WHERE bucket = ?1", }) } - fn delete_outdated_subscriptions(&self) -> Result<()> { - let now = self.now()?; + fn delete_outdated_subscriptions(&self, now: TimestampMicros) -> Result<()> { let stmt = self.db.prepare_v2("DELETE FROM ps_stream_subscriptions WHERE (expires_at < ?) OR (ttl IS NULL AND NOT active)")?; stmt.bind_int64(1, now.0)?; stmt.exec()?; @@ -403,8 +403,10 @@ WHERE bucket = ?1", } /// Increases the TTL for explicit subscriptions that are currently marked as active. - pub fn increase_ttl(&self, streams: &[StreamKey]) -> Result<()> { - let now = self.now()?; + /// + /// `now` must be the timestamp later passed to [Self::collect_subscription_requests], otherwise + /// a subscription with a zero TTL would expire between the two clock reads. + pub fn increase_ttl(&self, streams: &[StreamKey], now: TimestampMicros) -> Result<()> { let stmt = self.db.prepare_v2( "UPDATE ps_stream_subscriptions SET expires_at = ? + ttl * 1000000 WHERE stream_name = ? AND local_params = ? AND ttl IS NOT NULL", )?; @@ -705,3 +707,69 @@ pub struct PersistedBucketProgress<'a> { pub count_at_last: i64, pub count_since_last: i64, } + +// These tests need a linked SQLite library: `cargo test -p powersync_core --features static`. +#[cfg(all(test, feature = "static"))] +mod test { + use alloc::{string::ToString, vec}; + use core::ptr::null_mut; + + use powersync_sqlite_nostd::{self as sqlite}; + + use crate::{ + sqlite3_powersync_init, sync::subscriptions::StreamKey, utils::database::Database, + }; + + use super::StorageAdapter; + + #[link(name = "sqlite3")] + unsafe extern "C" {} + + fn open_database() -> Database { + let mut db = null_mut(); + assert_eq!(sqlite::open(c":memory:".as_ptr(), &mut db), 0); + let mut err_msg = null_mut(); + assert_eq!(sqlite3_powersync_init(db, &mut err_msg, null_mut()), 0); + + let db = Database::from(db); + db.exec_safe(c"BEGIN; SELECT powersync_init(); COMMIT;") + .unwrap(); + db + } + + /// Replaces the clock of the adapter with one advancing by a millisecond on every read. + fn use_ticking_clock(adapter: &mut StorageAdapter) { + let db = adapter.db; + db.exec_safe(c"CREATE TEMP TABLE test_clock AS SELECT 1740823200000000 AS now") + .unwrap(); + adapter.time_stmt = db + .prepare_v2("UPDATE test_clock SET now = now + 1000 RETURNING now") + .unwrap(); + } + + #[test] + fn active_subscription_with_zero_ttl_is_not_deleted() { + let db = open_database(); + let mut adapter = StorageAdapter::new(db).unwrap(); + use_ticking_clock(&mut adapter); + + db.exec_safe( + c"INSERT INTO ps_stream_subscriptions (stream_name, local_params, ttl, expires_at) VALUES ('a', 'null', 0, 0)", + ) + .unwrap(); + let active_streams = vec![StreamKey { + name: "a".to_string(), + params: None, + }]; + + let now = adapter.now().unwrap(); + adapter.increase_ttl(&active_streams, now).unwrap(); + let requested = adapter.collect_subscription_requests(true, now).unwrap(); + assert_eq!(requested.request.subscriptions.len(), 1); + + // Once the subscription is no longer active, it should expire. + let now = adapter.now().unwrap(); + let requested = adapter.collect_subscription_requests(true, now).unwrap(); + assert_eq!(requested.request.subscriptions.len(), 0); + } +} diff --git a/crates/core/src/sync/streaming_sync.rs b/crates/core/src/sync/streaming_sync.rs index 3955c15..3f009dc 100644 --- a/crates/core/src/sync/streaming_sync.rs +++ b/crates/core/src/sync/streaming_sync.rs @@ -238,7 +238,9 @@ impl StreamingSyncIteration { SyncStateMachineTransition::DataLineSaved { line: data_line } } SyncLine::KeepAlive(token) => { - self.adapter.increase_ttl(&self.options.active_streams)?; + let now = self.adapter.now()?; + self.adapter + .increase_ttl(&self.options.active_streams, now)?; if token.is_expired() { // Token expired already - stop the connection immediately. @@ -258,7 +260,7 @@ impl StreamingSyncIteration { // iteration if it has changed. let updated_request = self .adapter - .collect_subscription_requests(self.options.include_defaults)?; + .collect_subscription_requests(self.options.include_defaults, now)?; if updated_request.request != self.target.explicit_stream_subscriptions().request { @@ -393,10 +395,11 @@ impl StreamingSyncIteration { } SyncEvent::UploadFinished => self.try_applying_write_after_completed_upload(event)?, SyncEvent::DidUpdateSubscriptions { ref active_streams } => { - self.adapter.increase_ttl(&active_streams)?; + let now = self.adapter.now()?; + self.adapter.increase_ttl(&active_streams, now)?; let new_request = self .adapter - .collect_subscription_requests(self.options.include_defaults)?; + .collect_subscription_requests(self.options.include_defaults, now)?; if new_request.request != self.target.explicit_stream_subscriptions().request { // This changes stream requests, start another iteration. @@ -819,9 +822,10 @@ impl StreamingSyncIteration { let requests = adapter.collect_bucket_requests()?; let local_bucket_names: Vec = requests.iter().map(|s| s.name.clone()).collect(); - adapter.increase_ttl(&options.active_streams)?; + let now = adapter.now()?; + adapter.increase_ttl(&options.active_streams, now)?; let stream_subscriptions = - adapter.collect_subscription_requests(options.include_defaults)?; + adapter.collect_subscription_requests(options.include_defaults, now)?; let client_id = client_id(db)?; let checkpoint_request = if options.checkpoint_mode == CheckpointMode::Requests { diff --git a/dart/test/sync_stream_test.dart b/dart/test/sync_stream_test.dart index b062d94..8971784 100644 --- a/dart/test/sync_stream_test.dart +++ b/dart/test/sync_stream_test.dart @@ -428,6 +428,70 @@ void main() { expect(row, containsPair('expires_at', timestamp(plusMinutes: 30))); }); + syncTest('active subscriptions with a zero ttl are not deleted', (_) { + // Advance the clock on every read, so that two reads in the same + // operation never observe the same time. + var elapsed = Duration.zero; + final start = clock.now(); + final tickingClock = Clock(() { + elapsed += const Duration(milliseconds: 1); + return start.add(elapsed); + }); + + withClock(tickingClock, () { + const activeStreams = [ + {'name': 'my_stream'} + ]; + List storedSubscriptions() => + db.select('SELECT stream_name FROM ps_stream_subscriptions'); + + control( + 'subscriptions', + json.encode({ + 'subscribe': { + 'stream': {'name': 'my_stream'}, + 'ttl': 0, + } + }), + ); + + final startInstructions = + control('start', json.encode({'active_streams': activeStreams})); + expect( + startInstructions, + contains( + containsPair( + 'EstablishSyncStream', + containsPair( + 'request', + containsPair( + 'streams', + containsPair('subscriptions', [ + { + 'stream': 'my_stream', + 'parameters': null, + 'override_priority': null, + } + ]), + ), + ), + ), + ), + ); + expect(storedSubscriptions(), hasLength(1)); + + control('line_text', json.encode({'token_expires_in': 3600})); + expect(storedSubscriptions(), hasLength(1)); + + control('update_subscriptions', json.encode(activeStreams)); + expect(storedSubscriptions(), hasLength(1)); + + // Once the subscription is no longer active, it should expire. + control('update_subscriptions', json.encode([])); + expect(storedSubscriptions(), isEmpty); + }); + }); + syncTest('can be made implicit', (_) { control( 'subscriptions',