Skip to content

Commit ee9fcf9

Browse files
author
Yatao Li
committed
fix msgpack deserialization... why?
1 parent fe46347 commit ee9fcf9

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

Program.fs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ type MsgPackFormatter(resolver: IFormatterResolver) =
3737
member x.Deserialize(bytes: byte[] , offset: int, formatterResolver: IFormatterResolver , readSize: byref<int>) =
3838
if MessagePackBinary.GetMessagePackType(bytes, offset) = MessagePackType.Extension then
3939
let result = MessagePackBinary.ReadExtensionFormat(bytes, offset, &readSize)
40-
if result.TypeCode = 1y then
41-
let mutable _size = 0
42-
m_formatter.Deserialize(result.Data, 0, formatterResolver, &_size)
43-
else
44-
m_formatter.Deserialize(bytes, offset, formatterResolver, &readSize)
40+
let mutable _size = 0
41+
m_formatter.Deserialize(result.Data, 0, formatterResolver, &_size)
4542
else
46-
m_formatter.Deserialize(bytes, offset, formatterResolver, &readSize)
43+
m_formatter.Deserialize(bytes, offset, formatterResolver, &readSize)
4744

4845
type MsgPackResolver() =
4946
static let s_formatter = box(MsgPackFormatter(MessagePack.Resolvers.StandardResolver.Instance))

Properties/launchSettings.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"profiles": {
33
"fvim": {
4-
"commandName": "Project",
5-
"commandLineArgs": "--daemon"
4+
"commandName": "Project"
65
},
76
"norc": {
87
"commandName": "Project",

common.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ let newProcess prog args stderrenc =
162162
p.EnableRaisingEvents <- true
163163
// in case the parent crashed and we happen to be running Windows(tm)...
164164
// TODO need further investigation.
165-
ignore <| AppDomain.CurrentDomain.ProcessExit.Subscribe(fun _ -> p.Kill(true))
165+
ignore <| AppDomain.CurrentDomain.ProcessExit.Subscribe(fun _ -> try p.Kill(true) with _ -> ())
166166
p
167167

168168
[<AutoOpen>]

model.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,8 @@ let Start (serveropts, norc, debugMultigrid) =
527527
trace "commencing early initialization..."
528528

529529
async {
530+
do! Async.SwitchToNewThread()
531+
530532
let! api_info = Async.AwaitTask(nvim.call { method = "nvim_get_api_info"; parameters = [||] })
531533
let api_query_result =
532534
match api_info.result with
@@ -663,7 +665,9 @@ let Start (serveropts, norc, debugMultigrid) =
663665
if not norc then
664666
let! _ = Async.AwaitTask(nvim.command "if v:vim_did_enter | runtime! ginit.vim | else | execute \"autocmd VimEnter * runtime! ginit.vim\" | endif")
665667
()
666-
} |> Async.RunSynchronously
668+
}
669+
|> Async.Start
670+
|> ignore
667671

668672
let Flush =
669673
ev_flush.Publish

neovim.fs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ type Nvim() =
9090
if id < 0 then
9191
proc.Kill()
9292
failwithf "Remote daemon closed the connection with error code %d" id
93+
else
94+
trace "Connected to session %d" id
9395
TunneledSession proc
9496

9597
member this.start opts =
@@ -129,28 +131,33 @@ type Nvim() =
129131
let read (ob: IObserver<obj>) (cancel: CancellationToken) =
130132
Task.Factory.StartNew(fun () ->
131133
trace "begin read loop"
132-
let mutable ex = false
133-
while not ex && not cancel.IsCancellationRequested do
134+
let mutable channelClosed = false
135+
let mutable unhandledException = None
136+
while not channelClosed && not cancel.IsCancellationRequested && unhandledException.IsNone do
134137
try
135138
let data = MessagePackSerializer.Deserialize<obj>(stdout, true)
136139
ob.OnNext(data)
137140
with
138-
| :? InvalidOperationException
141+
| :? InvalidOperationException as _ex when _ex.Message = "Invalid MessagePack code was detected, code:-1"
142+
-> channelClosed <- true
139143
| :? System.IO.IOException
140144
| :? System.Net.Sockets.SocketException
141145
| :? ObjectDisposedException
142-
as _ex -> ex <- true
146+
-> channelClosed <- true
147+
| ex -> unhandledException <- Some ex
143148

144-
let ec = serverExitCode()
145-
if ec.IsSome then
146-
let code = ec.Value
149+
match serverExitCode(), unhandledException with
150+
| Some code, _ ->
147151
trace "end read loop: process exited, code = %d" code
148152
if code <> 0 then
149153
m_cancelSrc.Cancel()
150154
ob.OnNext([|box (Crash code)|])
151155
else
152156
ob.OnNext([|box Exit|])
153-
else
157+
| _, Some ex ->
158+
trace "end read loop: unhandled exception."
159+
ob.OnNext([|box (UnhandledException ex)|])
160+
| _ ->
154161
trace "end read loop."
155162
ob.OnNext([|box Exit|])
156163
ob.OnCompleted()

states.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Event =
3131
| Notification of nreq: Request
3232
| Error of emsg: string
3333
| Crash of ccode: int32
34+
| UnhandledException of ex: exn
3435
| Exit
3536

3637
let private _stateChangeEvent = Event<string>()
@@ -278,6 +279,8 @@ let msg_dispatch =
278279
trace "rpc" "neovim crashed with code %d" code
279280
_crashcode <- code
280281
failwithf "neovim crashed"
282+
| UnhandledException ex ->
283+
raise ex
281284
| other ->
282285
trace "rpc" "unrecognized event: %A" other
283286

0 commit comments

Comments
 (0)