15

I am getting following error in the phoenix webapp, I am developing. This is a simple app with few models and a page, where all models create form is added in a multi-tabbed form.

I am getting following error in the logs, However at browser's end things are working fine. From the logs also I am not able to understand what is wrong and how to debug this.

[error] #PID<0.1603.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /
** (exit) an exception was raised:
    ** (Plug.Conn.AlreadySentError) the response was already sent
        (plug) lib/plug/conn.ex:428: Plug.Conn.resp/3
        (plug) lib/plug/conn.ex:415: Plug.Conn.send_resp/3
        (my_app) web/controllers/personal_info_controller.ex:1: MyApp.PersonalInfoController.phoenix_controller_pipeline/2
        (my_app) lib/phoenix/router.ex:265: MyApp.Router.dispatch/2
        (my_app) web/router.ex:1: MyApp.Router.do_call/2
        (my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.phoenix_pipeline/1
        (my_app) lib/plug/debugger.ex:90: MyApp.Endpoint."call (overridable 3)"/2
        (my_app) lib/phoenix/endpoint/render_errors.ex:34: MyApp.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
Saurabh
  • 71,488
  • 40
  • 181
  • 244

2 Answers2

19

You get this error when you duplicate a response - this often happens when you have a plug that will send a response on some error (a user not being logged in for example) and then another response being sent from your controller action.

Without seeing your code it is hard to tell you where the problem is. After sending a response you can use Plug.Conn.halt/1 to prevent this error.

e.g.

conn
|> send_resp(404, "Post not found")
|> halt
Gazler
  • 83,029
  • 18
  • 279
  • 245
10

Removing plug :action from the controller fixed it for me. It is no longer needed as per https://github.com/phoenixframework/phoenix/issues/888

This solution was suggested on Phoenix/Elixir/Ejabberd - Response already sent error

Community
  • 1
  • 1
neildaemond
  • 656
  • 1
  • 8
  • 17