Skip to content

Commit 86e0a6e

Browse files
Merge pull request #14 from Lercher/omnisharp-input-thread
ProcessInputThread should be sync
2 parents 91b1961 + 5c3c5da commit 86e0a6e

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/JsonRpc/InputHandler.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ public void Start()
5353
_scheduler.Start();
5454
}
5555

56-
private async void ProcessInputStream()
56+
// don't be async: We already allocated a seperate thread for this.
57+
private void ProcessInputStream()
5758
{
59+
// some time to attach a debugger
60+
// System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));
61+
5862
// header is encoded in ASCII
5963
// "Content-Length: 0" counts bytes for the following content
6064
// content is encoded in UTF-8
@@ -63,13 +67,13 @@ private async void ProcessInputStream()
6367
if (_inputThread == null) return;
6468

6569
var buffer = new byte[300];
66-
var current = await _input.ReadAsync(buffer, 0, MinBuffer);
70+
var current = _input.Read(buffer, 0, MinBuffer);
6771
if (current == 0) return; // no more _input
6872
while (current < MinBuffer ||
6973
buffer[current - 4] != CR || buffer[current - 3] != LF ||
7074
buffer[current - 2] != CR || buffer[current - 1] != LF)
7175
{
72-
var n = await _input.ReadAsync(buffer, current, 1);
76+
var n = _input.Read(buffer, current, 1);
7377
if (n == 0) return; // no more _input, mitigates endless loop here.
7478
current += n;
7579
}
@@ -99,7 +103,7 @@ private async void ProcessInputStream()
99103
var received = 0;
100104
while (received < length)
101105
{
102-
var n = await _input.ReadAsync(requestBuffer, received, requestBuffer.Length - received);
106+
var n = _input.Read(requestBuffer, received, requestBuffer.Length - received);
103107
if (n == 0) return; // no more _input
104108
received += n;
105109
}

test/JsonRpc.Tests/InputHandlerTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,34 @@ public void ShouldPassInRequests()
7070
}
7171
}
7272

73+
[Fact]
74+
public void ShouldHaveAThreadName()
75+
{
76+
var threadName = "(untouched)";
77+
var inputStream = new MemoryStream(Encoding.ASCII.GetBytes("Content-Length: 2\r\n\r\n{}"));
78+
var reciever = Substitute.For<IReciever>();
79+
80+
using (NewHandler(
81+
inputStream,
82+
Substitute.For<IOutputHandler>(),
83+
reciever,
84+
Substitute.For<IRequestProcessIdentifier>(),
85+
Substitute.For<IRequestRouter>(),
86+
Substitute.For<IResponseRouter>(),
87+
cts => {
88+
reciever.When(x => x.IsValid(Arg.Any<JToken>()))
89+
.Do(x => {
90+
threadName = System.Threading.Thread.CurrentThread.Name;
91+
cts.Cancel();
92+
});
93+
}))
94+
{
95+
reciever.Received();
96+
threadName.Should().Be("ProcessInputStream", because: "it is easier to find it in the Threads pane by it's name");
97+
}
98+
99+
}
100+
73101
[Fact]
74102
public void ShouldPassInUtf8EncodedRequests()
75103
{

0 commit comments

Comments
 (0)