Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [Bug]: Can't close websocket connection #698

Open
3 tasks done
mxgnus-de opened this issue Jul 26, 2023 · 7 comments
Open
3 tasks done

🐛 [Bug]: Can't close websocket connection #698

mxgnus-de opened this issue Jul 26, 2023 · 7 comments
Labels
☢️ Bug Something isn't working

Comments

@mxgnus-de
Copy link

mxgnus-de commented Jul 26, 2023

Bug Description

I want to close a websocket connection but nothing happens (no error etc.).
The client remains connected and can also send messages.

How to Reproduce

Steps to reproduce the behavior:

  1. Run the code below
  2. Connect to the server with a websocket client

Expected Behavior

The server should close the connection after 5 seconds

Contrib package Version

websocket/v1.1.0

Code Snippet (optional)

package main

import (
	"log"
	"time"

	"github.com/gofiber/contrib/websocket"
	"github.com/gofiber/fiber/v2"
)

func main() {
	app := fiber.New()
	app.Use("/ws", middleware)
	app.Get("/ws", websocket.New(handler))
	log.Fatal(app.Listen(":8080"))
}

func middleware(c *fiber.Ctx) error {
	if websocket.IsWebSocketUpgrade(c) {
		return c.Next()
	}
	return fiber.ErrUpgradeRequired
}

func handler(conn *websocket.Conn) {
	go func() {
		time.Sleep(5 * time.Second)
		if err := conn.Close(); err != nil {
			log.Fatal(err)
		}
	}()

	for {
		_, msg, err := conn.ReadMessage()
		if err != nil {
			log.Println(err)
			return
		}
		log.Printf("msg: %s", msg)
	}
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.
@mxgnus-de mxgnus-de added the ☢️ Bug Something isn't working label Jul 26, 2023
@gaby
Copy link
Member

gaby commented Jul 28, 2023

@mstrYoda Any idea what may be causing this?

@mstrYoda
Copy link
Member

Hi, you can not close a hijacked connection in this way. It is basically do nothings.

You can just return from the websocket handler function and it closes underlying connection after exiting handler.

@mxgnus-de
Copy link
Author

Returning directly from the websocket handler worked.
But for what purpose is the (*websocket.Conn).Close() func if it can't close the connection?

@dev-tathkarah
Copy link

I am facing the same issue here. Even after i closed the *Conn. The postman client still holds the connection and can send messages to server. I am pooling my socket connections in cache and then writing to it, So returning from the function is practically not possible for me. Your assistance in this matter is highly appreciated

@ReneWerner87
Copy link
Member

@dev-tathkarah @mxgnus-de
https://github.com/fasthttp/websocket/blob/82c80177346b0f5f86cad5f8d20d81e2ec89e70b/conn.go#L346-L354

what is the return of the function ? is it returning an error ?
we use https://github.com/fasthttp/websocket internally
maybe we should make an report there ?

@ReneWerner87
Copy link
Member

maybe releated to fasthttp/websocket#39

@dev-tathkarah
Copy link

dev-tathkarah commented Jan 29, 2025

@ReneWerner87 Close() is returning nil and i perform some cleanup activites like clearing the socket connection from cache. But the underlying connection is not closed

I was expecting the websocket to behave in a way that when we call conn.Close() then the connection will be closed including whatever the connections holds under the hood should be terminated

but the connection still remains in the client and client can still send the message into socket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
☢️ Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants