Unable to connect to gRPC server running locally

Hi Team,

I am totally new to K6 and running the tests on gRPC for the very first time. I was able to run a sample k6 script and sample gRPC application and there were no issues.

However, as I am in the process of running the script against my actual application, I am getting below error.

{"value":{"desc":"transport: error while dialing: dial tcp 127.0.0.1:9191: connect: can't assign requested address"}}  source=console

Below is how my script looks like.

// Basic import statements
import grpc from 'k6/net/grpc';
import {check} from 'k6';
import { Counter } from 'k6/metrics';


// Creating grpc client
const client= new grpc.Client();

// Loading the grpc file into this client
client.load(['../proto/elastic'], 'protoFileName.proto');

//define the load test patterns.
// currently targetting the test to run for 5 mins with 50 users
export const options ={
    stages:[
        {duration: "1s", target: 1}, // ramp-up 50 users in 25 sec
        {duration: "25s", target: 1}, // steady state for 5 mins with 50 users
        {duration: "1s", target: 0} // ramp-down all users in 25 sec
    ]
}

// Create all the counters here. For now I am only creating the error counter
const errorCounter= new Counter("Connection_Errors");

// Write the default export fuction.
export default() => {
    // Make the connection in try catch block
    try{
        console.log("Trying to connect");
        client.connect("127.0.0.1:9191",{
            plaintext:true,
        });
    }catch(err){
        console.log("Inside Catch");
        console.log(err); // We can remove this statement at some time.
        errorCounter.add(1);
        console.log("Inside catch ends");
    }

    // Assuming the connection is successful, invoke the service in the proto file.

    // First create the test data that is required. This may be hardcoaded, but will 
    const params= {param1 : "SomeValue",
       param2: "SomeValue",
       param3: "SomeValue"
    };

    // Invoke the URL now
    const response = client.invoke("CLIENT_PACKAGE_FROM_PROTO.SERVICE_NAME/FinishExecution", params);

    //Check the response 
    check(response,{
        "FinishExecutionSuccess": (r)=> r.status=== grpc.StatusOK,

    });

    // Finally close the connection.
    client.close();
}

Can someone please give me some pointers in terms of how can I debug and solve this issue? I ran netstat command and checked that there is a server listening on port 9191. So I am unable to understand why the connection is not being made.

Hi @Abhiram.Pattarkine, Welcome to the community forum!

Can you confirm that neither the server nor k6 is ran within a docker container?

connect: can’t assing requested address

Seems like a very strange error to me and seems to me different variants of your network isn’t correctly setup (or too securely setup in some cases).

We have a previous topic on this Connect: cannot assign requested address, which doesn’t really seems to have gone anywhere as well.

Can you confirm that you can use anything else to connect to 127.0.0.1:9191 from your machine? Maybe you don’t have 127.0.0.1 even defined :person_shrugging:

1 Like

Hi @mstoykov Thanks for your response.

To answer your questions, Neither the server nor k6 is running within the docker container.

I am connected to VPN, but tried both options i.e. connecting and disconnecting, but they did not work.

Strange thing is, it is showing the exception in connecting to above server, it returns correct response to the client.invoke() call. And then again throw the error as below.

ERRO[0024] GoError: no gRPC connection, you must call connect first

I did see the thread you mentioned above but like you said, did not make any progress.

I am able to run the sample service from Postman gRPC and I don’t see any “Connection Errors” as such from postman. I am not sure if Postman is establishing a connection.

@Abhiram.Pattarkine

Can you tell us how you have installed k6 and what version. For the later you can run k6 version

Hi @mstoykov

Below is the version I am running.

k6 v0.44.1 ((devel), go1.20.4, darwin/arm64)

(devel) in this means it was build by source usually.

Have you installed with brew or did you compile it from source?

My suspicion was that you were using snaps or some other app container technology to install. But not certain if those even are available on macos.

Unfortunately I don’t really have any experience with MacOS and some google searches mostly suggest we could be running out of ephemeral ports. Which is not impossible. But with 1 VU it is very unlikely.

You can look at this article for some commands to check what the limit is in your case.

But I would expect this to become a problem after you have run a bunch of iterations with a lot of VUs not with just 1 VU.

Can you run a test against https://grpcbin.test.k6.io/ ? Does that work without problems?

3 Likes

That is interesting theory @mstoykov and I think that makes sense to me. I do see a lot of connections in TIME_WAIT state after a while. I also checked the duration of TIME_WAIT state by running the sysctl net.inet.tcp.msl command and it gives 15000 as response as mentioned in the article.

Since the default value of ConnectParams.timeout is 60s, I assumed, it should be sufficient time to reuse the connection. I ran the test with higher number of user like you mentioned above and it looks okay with some occasional errors.

Also ran some tests on hello world app on grpcbin server. That test runs fine. That further solidifies your theory. I am thinking, since I am closing the connection at the end using client.close() call, it goes in TIME_WAIT. I will see if there is a way to close the connection at the end of the test (something like VUSER_END state).

For now, I will mark this as solution. Will reopen this thread if problem still persist.