diff --git a/ldclient/impl/datasource/async_streaming.py b/ldclient/impl/datasource/async_streaming.py index fb3c5bc5..bc42ee0c 100644 --- a/ldclient/impl/datasource/async_streaming.py +++ b/ldclient/impl/datasource/async_streaming.py @@ -218,6 +218,8 @@ async def _process_message(self, msg: Event) -> bool: path = payload['path'] # noinspection PyShadowingNames version = payload['version'] + if isinstance(version, bool) or not isinstance(version, int): + raise ValueError("delete event for %s has a version that is not an integer" % path) log.debug("Received delete event for %s, New version: [%d]", path, version) parsed = parse_path(path) if parsed is not None: diff --git a/ldclient/impl/datasource/streaming.py b/ldclient/impl/datasource/streaming.py index 9e425e3f..25002a7e 100644 --- a/ldclient/impl/datasource/streaming.py +++ b/ldclient/impl/datasource/streaming.py @@ -206,6 +206,8 @@ def _process_message(self, store, msg: Event) -> bool: path = payload['path'] # noinspection PyShadowingNames version = payload['version'] + if isinstance(version, bool) or not isinstance(version, int): + raise ValueError("delete event for %s has a version that is not an integer" % path) log.debug("Received delete event for %s, New version: [%d]", path, version) target = parse_path(path) if target is not None: diff --git a/ldclient/testing/impl/datasource/test_async_streaming.py b/ldclient/testing/impl/datasource/test_async_streaming.py index 302a2a3c..625fc468 100644 --- a/ldclient/testing/impl/datasource/test_async_streaming.py +++ b/ldclient/testing/impl/datasource/test_async_streaming.py @@ -279,6 +279,20 @@ async def test_delete_event_removes_item_from_store(): await proc.stop() +@pytest.mark.asyncio +async def test_delete_event_with_version_that_is_not_an_integer_restarts_stream(): + put_data = _make_put_data() + delete_data = _make_delete_data(FEATURES, 'f1', 'no') + actions = [_start(), _event('put', put_data), _event('delete', delete_data)] + + # Run the processor until it handles the delete. + proc, store, ready, factory = await _run_with_actions(actions, retry_state=_fast_retry_state()) + # The processor drops the stream. + await _wait_until(lambda: factory.created[0].interrupted) + + await proc.stop() + + @pytest.mark.asyncio async def test_fault_with_error_does_not_set_ready_by_itself(): """A Fault that arrives before any put must not mark the processor as initialized.""" diff --git a/ldclient/testing/impl/datasource/test_streaming.py b/ldclient/testing/impl/datasource/test_streaming.py index 0fa3b803..6de65365 100644 --- a/ldclient/testing/impl/datasource/test_streaming.py +++ b/ldclient/testing/impl/datasource/test_streaming.py @@ -234,6 +234,38 @@ def test_receives_delete_events(): expect_delete(store, SEGMENTS, segmentv1['key']) +def test_restarts_stream_if_delete_event_version_is_not_an_integer(): + store = InMemoryFeatureStore() + ready = Event() + flagv1 = FlagBuilder('flagkey').version(1).build() + + with start_server() as server: + with stream_content(make_put_event()) as stream1, stream_content(make_put_event([flagv1])) as stream2: + config = Config(sdk_key='sdk-key', stream_uri=server.uri, initial_reconnect_delay=brief_delay) + + listeners = Listeners() + + def listener(s): + # The test server serves one request at a time, so end the first stream when the SDK reports an error. + if s.error is not None: + stream1.close() + + listeners.add(listener) + + config._data_source_update_sink = DataSourceUpdateSinkImpl(store, listeners, Listeners()) + server.for_path('/all', SequentialHandler(stream1, stream2)) + + with StreamingUpdateProcessor(config, store, ready, None) as sp: + sp.start() + ready.wait(start_wait) + assert sp.initialized() + + # Send a delete whose version is a string. + stream1.push(make_delete_event(FEATURES, flagv1['key'], 'no')) + # The SDK drops the stream and gets the data again on a new connection. + expect_update(store, FEATURES, flagv1) + + def test_reconnects_if_stream_is_broken(): store = InMemoryFeatureStore() ready = Event()