Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 73 additions & 5 deletions crates/core/src/sync/storage_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,9 @@ WHERE bucket = ?1",
pub fn collect_subscription_requests(
&self,
include_defaults: bool,
now: TimestampMicros,
) -> Result<RequestedStreamSubscriptions> {
self.delete_outdated_subscriptions()?;
self.delete_outdated_subscriptions(now)?;

let mut subscriptions: Vec<RequestedStreamSubscription> = Vec::new();
let mut index_to_local_id = Vec::<i64>::new();
Expand Down Expand Up @@ -394,17 +395,18 @@ 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()?;
Ok(())
}

/// 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",
)?;
Expand Down Expand Up @@ -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"))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test this through Dart instead? We're already able to mock the time there, so we wouldn't have to switch out the statement in the adapter.

Adding a test to sync_stream_test.dart that does something like this should work:

syncTest('uses consistent timestamps', (_) {
  final initialTime = DateTime.utc(2026, 11, 13, 10);
  var counter = 0;
  withClock(Clock(() => initialTime.add(Duration(seconds: counter++))), () {
    // Subscribe to streams and add keepalive lines here
  });
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have read the entire thing first. Given that we already have the Dart test, I don't think we need this. Our Rust unit tests don't link SQLite currently, all database tests are running through Dart.

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);
}
}
16 changes: 10 additions & 6 deletions crates/core/src/sync/streaming_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -819,9 +822,10 @@ impl StreamingSyncIteration {

let requests = adapter.collect_bucket_requests()?;
let local_bucket_names: Vec<String> = 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 {
Expand Down
64 changes: 64 additions & 0 deletions dart/test/sync_stream_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object?> 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',
Expand Down