Skip to content

RowStream.next() returns fewer results than expected when iterating multiple rows #281

@JPC-007

Description

@JPC-007

Description

When using result.next().await to iterate over query results that return multiple rows, the iteration terminates prematurely, returning fewer results than expected. Single-row queries work correctly.

Environment

  • neo4rs version: 0.9.0-rc.9
  • Neo4j version: 5.x (running in Kubernetes)
  • Rust version: 1.81+
  • OS: Linux (container)

Steps to Reproduce

  1. Create multiple FOLLOWS relationships in Neo4j:
CREATE (a:User {id: "user-1"})-[:FOLLOWS]->(b:User {id: "user-2"})
CREATE (a:User {id: "user-1"})-[:FOLLOWS]->(c:User {id: "user-3"})
CREATE (a:User {id: "user-1"})-[:FOLLOWS]->(d:User {id: "user-4"})
CREATE (a:User {id: "user-1"})-[:FOLLOWS]->(e:User {id: "user-5"})
CREATE (a:User {id: "user-1"})-[:FOLLOWS]->(f:User {id: "user-6"})
  1. Query using neo4rs:
let cypher = r#"
    MATCH (user:User {id: $user_id})-[:FOLLOWS]->(following:User)
    RETURN following.id AS following_id
    ORDER BY following.id
    SKIP $offset
    LIMIT $limit
"#;

let mut result = graph
    .execute(
        query(cypher)
            .param("user_id", "user-1")
            .param("offset", 0i64)
            .param("limit", 50i64),
    )
    .await?;

let mut following = Vec::new();
while let Some(row) = result.next().await? {
    if let Ok(id_str) = row.get::<String>("following_id") {
        following.push(id_str);
    }
}

println!("Expected: 5, Got: {}", following.len());

Expected Behavior

The loop should iterate 5 times and return all 5 following IDs.

Actual Behavior

The loop only iterates 1 time and returns only 1 result, even though:

  • The same query in cypher-shell returns all 5 results
  • A separate COUNT query returns total_count = 5
  • has_more is calculated as true (indicating more data exists)

Additional Context

What works correctly:

  • Single-row queries (e.g., RETURN count(r) > 0 AS is_following) work fine
  • The same data queried via PostgreSQL returns all 5 results

Possibly related:

Workaround

We've switched to using PostgreSQL for list queries while keeping Neo4j for single-value lookups and graph traversals.

Code Reference

Our repository pattern:

pub struct GraphRepository {
    graph: Arc<Graph>,
}

impl GraphRepository {
    pub async fn new(uri: &str, user: &str, password: &str) -> Result<Self> {
        let graph = Graph::new(uri, user, password)?;
        Ok(Self { graph: Arc::new(graph) })
    }
}

The Graph is wrapped in Arc and shared across async tasks. Could this be related to connection pool state issues?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions