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

net/http: should get "ok" but get no response #23336

Closed
anotherGoogleFan opened this issue Jan 4, 2018 · 4 comments
Closed

net/http: should get "ok" but get no response #23336

anotherGoogleFan opened this issue Jan 4, 2018 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@anotherGoogleFan
Copy link

anotherGoogleFan commented Jan 4, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.9.2 linux/amd64
go version go1.9.2 windows/amd64
both got the same problem

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

one is windows:
$ go env
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\GoProject
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config

one is linux:
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/GoProject"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build038803651=/tmp/go-build"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

code is simple enough:

package main

import (
	"context"
	"net/http"
	"os"
	"os/signal"
	"time"

	"github.com/anotherGoogleFan/log"
)

func main() {
	mux := http.NewServeMux()
	mux.Handle("/video/upload", http.HandlerFunc(UploadVideo))
	srv := &http.Server{
		Addr:    ":8848",
		Handler: mux,
	}
	// 如果接收到关闭信号(如 kill -2)则进行graceful shutdown
	go gracefulShutdown(srv)

	// service connections
	if err := srv.ListenAndServe(); err != nil {
		log.Fields{
			"port": "8848",
			"err":  err.Error(),
		}.Error("unexpected service stop")
		return
	}
}

func gracefulShutdown(srv *http.Server) {
	stopChan := make(chan os.Signal)
	signal.Notify(stopChan, os.Interrupt, os.Kill)
	<-stopChan
	log.Info("receive shutdown signal. Ready to exist")
	defer log.Info("program exists")
	ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
	srv.Shutdown(ctx)
}

func UploadVideo(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		http.Error(w, "only post allowed", http.StatusMethodNotAllowed)
		return
	}

	http.Error(w, "ok", http.StatusOK)
}

What did you expect to see?

I post a file
uploadwithnoresponse
with chrome plugin postman. I should get the response "ok".

What did you see instead?

No response.

@mvdan
Copy link
Member

mvdan commented Jan 4, 2018

Please see https://github.com/golang/go/wiki/Questions for a more appropriate place to ask questions. I don't see a specific bug report here - just a program that isn't doing what you wanted it to do.

If there is a bug, please clarify what it is and give a short, concise program to reproduce it.

@mvdan mvdan added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jan 4, 2018
@brenol
Copy link

brenol commented Jan 5, 2018

This issue seems to happen in when using Postman only.

  • I was able to reproduce in Windows
  • I was able to reproduce in Linux (Ubuntu 17.10)
  • The file being sent must be at least over 700KB from what I have tested
  • If the file is over 700KB, that message is shown most times when using Postman
  • I was unable to reproduce this using curl

Minimal repro: https://play.golang.org/p/UQ6AVD5Lqlj

Seems to me Postman is doing something with the connection.

Tests were made using Go1.9.2/Windows & Go1.9.2/Linux

@katakonst
Copy link

katakonst commented Jan 7, 2018

You need to read the body. From what i see it exists a limit for unread request body data.

// maxPostHandlerReadBytes is the max number of Request.Body bytes not
// consumed by a handler that the server will read from the client
// in order to keep a connection alive. If there are more bytes than
// this then the server to be paranoid instead sends a "Connection:
// close" response.
//
// This number is approximately what a typical machine's TCP buffer
// size is anyway.  (if we have the bytes on the machine, we might as
// well read them)
const maxPostHandlerReadBytes = 256 << 10

A quick solution for your code can be

func UploadVideo(w http.ResponseWriter, r *http.Request) {
	io.Copy(ioutil.Discard, r.Body)
	if r.Method != http.MethodPost {
		http.Error(w, "only post allowed", http.StatusMethodNotAllowed)
		return
	}

	http.Error(w, "ok", http.StatusOK)
}

@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Feb 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants