elixir-mint / mint_web_socket

HTTP/1 and HTTP/2 WebSocket support for Mint 🌱
https://hex.pm/packages/mint_web_socket
Apache License 2.0
124 stars 14 forks source link

Dialyzer error "Function call without opaqueness type mismatch." #30

Closed mruoss closed 1 year ago

mruoss commented 1 year ago

Hi there

In my codebase I'mm defining the following function to upgrade a connection:

  def upgrade(conn, path, headers) do
    {:ok, conn, ref} = Mint.WebSocket.upgrade(:wss, conn, path, headers)

    {:ok, conn, [{:status, ^ref, status}, {:headers, ^ref, resp_headers} | _]} =
      receive(do: (msg -> Mint.WebSocket.stream(conn, msg)))

    Mint.WebSocket.new(conn, ref, status, resp_headers)
  end

Running dialyzer, I get the following feedback:

Function call without opaqueness type mismatch.

Call does not have expected opaque term of type Mint.HTTP.t() in the 1st position.

Mint.WebSocket.stream(_conn :: Mint.HTTP1.t() | Mint.HTTP2.t(), _msg :: any())

I tried to figure out why this happens and I think it's got to do with the Mint.WebSocket.upgrade/5 function. Although its spec looks fine, if I ommit that function call, dialyer seems happy. Any ideas?

the-mikedavis commented 1 year ago

I tried to reproduce this in a fresh project (mix new mwstest) with this module:

defmodule Mwstest do
  def new_and_upgrade do
    {:ok, conn} = Mint.HTTP.connect(:https, "localhost", 9_000)
    upgrade(conn, "/socket", [])
  end

  def upgrade(conn, path, headers) do
    {:ok, conn, ref} = Mint.WebSocket.upgrade(:wss, conn, path, headers)

    {:ok, conn, [{:status, ^ref, status}, {:headers, ^ref, resp_headers} | _]} =
      receive(do: (msg -> Mint.WebSocket.stream(conn, msg)))

    Mint.WebSocket.new(conn, ref, status, resp_headers)
  end
end

but the dialyzer passes successfully.

Can you provide a minimal project that triggers the error? Also - what versions of Mint, Mint.WebSocket, Elixir and OTP are you running?

mruoss commented 1 year ago

Oh your're right... very strange. I can't reproduce it on a fresh project either!

Mint: 1.4.2 Mint.WebSocket: 1.0.1 Elixir: 1.14.1 OTP: 25.0.4

I'll spend some more time figuring this out.

mruoss commented 1 year ago

Deleted the plts, error is gone. I got other errors but they might be fixed with #27

mruoss commented 1 year ago

Indeed, if I use the main branch, no more dialyzer problems.

the-mikedavis commented 1 year ago

I'll cut a patch release so the dialyzer fixes are published 👍

mruoss commented 1 year ago

That's perfect, thank you. I really like this module, thank you for your efforts! I'm currently using it here: https://github.com/coryodaniel/k8s/pull/199