question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sending a SyncRequest from within a SyncRequest and getting a response

See original GitHub issue

Hey!

I’m in a situation where I would need this to work:

  1. Client sends a SyncRequest to Server
  2. Server requires more info and therefore sends a SyncRequest back to Client
  3. Client replies with a SyncResponse including the requested info
  4. Server replies to the original SyncRequest (from step 1)

Basically: client -> server (extra: -> client -> server) -> client, if that makes sense.

To explain it better, here is some code to illustrate what I’d want to do (fiddle):

using System.Text;
using WatsonTcp;

const string host = "127.0.0.1";
const int port = 1337;

var server = new WatsonTcpServer(host, port);
var client = new WatsonTcpClient(host, port);

server.Callbacks.SyncRequestReceived += request =>
{
    Console.WriteLine(2);
    var response = server.SendAndWait(1000, request.IpPort, "hello from server!");
    
    Console.WriteLine(4);
    return new SyncResponse(request, response.Data);
};

client.Callbacks.SyncRequestReceived += request =>
{
    Console.WriteLine(3);
    return new SyncResponse(request, "hello back from client!");
};

server.Events.MessageReceived += (_, _) => { };
client.Events.MessageReceived += (_, _) => { };

server.Start();
client.Connect();

Console.WriteLine(1);
var response = client.SendAndWait(1000, "hello from client!");

Console.WriteLine("client received response: " + Encoding.UTF8.GetString(response.Data));
Console.ReadKey(true);

However, this is the output I am getting from this exact code:

1
2
3
Unhandled exception. System.TimeoutException: A response to a synchronous request was not received within the timeout window.

As you can see, the client does not seem to be sending a response back, or at least the server isn’t letting me know about it (stuck at step 3). If I was to guess, and without having read the entire codebase, I would assume this is the intended behavior and is due to a lock() or SemaphoreSlim somewhere, preventing concurrent requests/responses?

This would make sense as otherwise, it would mean multiple different data could be received concurrently, mixing everything up. Hopefully I’m not saying sh*t here. Please, correct me if so 😅 Always looking to get more knowledge!

Considering I am right (and even if I am not), then how would I go about getting this to work?

What I thought of is: Instead of sending a SyncRequest back from within a SyncRequest, what I could do is make the server reply right away with a SyncResponse saying something along the lines of "hey! more data is required! please come back with a new SyncRequest containing it!", but that is pretty inconvenient and would not work well with my current structure. My question here really is about (issue title): “Sending a SyncRequest from within a SyncRequest and getting a response”, without getting blocked.

So, what can/should I do? Is that even possible at all or should I completely forget about it and instead go with the idea above?

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
jchristncommented, Nov 28, 2022

Thanks Matt! Will be moving over to the other issues shortly! Cheers

1reaction
Laiteuxcommented, Nov 28, 2022

Anyway, everything now seems to work just fine. I’ll close this issue so we can move to the next one whenever you have some time.

Thanks a lot for your time collaborating on this, it is very much appreciated. Please keep up the good work! (:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Client disconnection only in SyncRequest after ...
Hi, I'm getting in a strange problem, I'll describe by steps: Client connects fine and sends messages and syncRequest correctly Client sends ......
Read more >
Sync request and the ConfirmBOD response
Sync is used to notify interested parties the current state of the business objects that the system manages. Only the system that contains...
Read more >
Generating and Sending Messages
Use the IntBroker class SyncRequest method for handling outbound synchronous transfers. To send a message synchronously, you can employ SyncRequest in: The ...
Read more >
Sync request/response messaging using azure functions ...
The issue comes down to azure function being able to work (Bind & Trigger) with two different queues, one sending messages (bind) to...
Read more >
Synchronous and asynchronous requests - Web APIs | MDN
XMLHttpRequest supports both synchronous and asynchronous communications. In general, however, asynchronous requests should be preferred to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found