Skip to content

Matrix client hangs and exposes confusing API #2553

Closed

Description

@easbar

In a fresh maven/Java project:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>client-hc-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.graphhopper</groupId>
            <artifactId>directions-api-client-hc</artifactId>
            <version>5.0</version>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

Main.java (change the key)

import com.graphhopper.api.GHMRequest;
import com.graphhopper.api.GraphHopperMatrixWeb;
import com.graphhopper.api.MatrixResponse;
import com.graphhopper.util.shapes.GHPoint;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        GraphHopperMatrixWeb matrixClient = new GraphHopperMatrixWeb();
        matrixClient.setKey("your_key");
        GHMRequest ghmRequest = new GHMRequest();
        ghmRequest.addFromPoint(new GHPoint(49.6724, 11.3494));
        for (GHPoint to : Arrays.asList(new GHPoint(49.6724, 11.3494), new GHPoint(49.6550, 11.4180))) {
            ghmRequest.addToPoint(to);
        }
        MatrixResponse response = matrixClient.route(ghmRequest);
        if (response.hasErrors())
            throw new RuntimeException(response.getErrors().toString());
        System.out.println("it's over... but apparently it's not...");
    }
}
mvn compile
mvn exec:java -Dexec.mainClass="Main"

-> prints "it's over... but apparently it's not...", but then the process never returns.
Pausing the program with a debugger showed that it hangs in SocketInputStream#socketRead0.
I tried this with Java 8, 11 and 17.

The GHMRequest API is quite confusing: setProfile does not set the profile. This does nothing:

GHMRequest ghmRequest = new GHMRequest();
ghmRequest.setProfile("small_truck");

setPoints does not set the points. This does nothing either.

GHMRequest ghmRequest = new GHMRequest();
ghmRequest.setPoints(points);

It seems very strange that GHMRequest extends from GHRequest. This is probably the reason setProfile and setPoints do not work. Apparently we have to use addPoint,addAllPoint etc. instead.

Activity

  1. added this to the 6.0 milestone on Apr 6, 2022
  2. karussell commented on Apr 7, 2022

    @karussell
    Member

    The /route endpoint has the same problem. But not for localhost. When switching to http1.1 it returns immediately:

    OkHttpClient httpClient = new OkHttpClient.Builder().protocols( Collections.singletonList(Protocol.HTTP_1_1)).build();
    GraphHopperMatrixWeb matrixClient = new GraphHopperMatrixWeb(new GHMatrixSyncRequester("https://graphhopper.com/api/1/matrix", httpClient, true));

    So the hanging could be an http2 problem of okhttp or a server side problem where the http2 connection isn't closed (something like: the server thinks the connection is just idle (-> hence the timeout after 3m) and the client thinks it is already done).

    update: I tried a simple rust program

    use isahc::prelude::*;
    
    fn main() -> Result<(), isahc::Error> {
    
      let mut response = isahc::get("https://graphhopper.com/api/1/route?point=52.508699,13.40744&point=52.491979,13.465118&key=your_key")?;
    
      println!("Status: {}", response.status());
      println!("Headers: {:#?}", response.headers());
    
      print!("{}", response.text()?);
      Ok(())    
    }

    and it did not hang although it should support http2 by default. Same with our JavaScript client. So it is more likely some okhttp problem.

    The okhttp-only version is as follows and hangs:

    public static void main(String[] args) throws IOException {
        OkHttpClient httpClient = new OkHttpClient();
    // OkHttpClient httpClient = new OkHttpClient.Builder().protocols( Collections.singletonList(Protocol.HTTP_1_1)).build(); // method 1 to avoid hanging
        Request req = new Request.Builder().url("https://graphhopper.com/api/1/route?point=52.508699,13.40744&point=52.491979,13.465118&key=your_key").
           // header("Connection", "close"). // method 2 to avoid hanging
           build();
        ResponseBody body = httpClient.newCall(req).execute().body();
        System.out.println(body.string());
        body.close();
        System.out.println("it's over... but apparently it's not...");
    }
  3. karussell commented on Apr 10, 2022

    @karussell
    Member

    Upgrading to the latest 4.9.3 okhttp seems to fix this hanging problem (4.1.0 has the same problem, but >=4.2.0 is fine). (This 4.x version adds a kotlin lib as dependency)

    Can you confirm this?

  4. easbar commented on Apr 17, 2022

    @easbar
    MemberAuthor

    I tried, but I get slightly different results:

    • 4.2.0-4.2.2 all hang
    • 4.3.0 does not hang
    • 4.9.3 does not hang either
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions