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

Disable lineLimitReader when handle BDAT data #139

Merged
merged 7 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ type Conn struct {
locker sync.Mutex
binarymime bool

bdatPipe *io.PipeWriter
bdatStatus *statusCollector // used for BDAT on LMTP
dataResult chan error
bytesReceived int // counts total size of chunks when BDAT is used
lineLimitReader *lineLimitReader
bdatPipe *io.PipeWriter
bdatStatus *statusCollector // used for BDAT on LMTP
dataResult chan error
bytesReceived int // counts total size of chunks when BDAT is used

fromReceived bool
recipients []string
Expand All @@ -60,15 +61,16 @@ func newConn(c net.Conn, s *Server) *Conn {
}

func (c *Conn) init() {
c.lineLimitReader = &lineLimitReader{
R: c.conn,
LineLimit: c.server.MaxLineLength,
}
rwc := struct {
io.Reader
io.Writer
io.Closer
}{
Reader: &lineLimitReader{
R: c.conn,
LineLimit: c.server.MaxLineLength,
},
Reader: c.lineLimitReader,
Writer: c.conn,
Closer: c.conn,
}
Expand Down Expand Up @@ -739,6 +741,8 @@ func (c *Conn) handleBdat(arg string) {
}()
}

prevLineLimit := c.lineLimitReader.LineLimit
c.lineLimitReader.LineLimit = 0
chunk := io.LimitReader(c.text.R, int64(size))
_, err = io.Copy(c.bdatPipe, chunk)
if err != nil {
Expand All @@ -753,6 +757,7 @@ func (c *Conn) handleBdat(arg string) {
}

c.reset()
c.lineLimitReader.LineLimit = prevLineLimit
Copy link
Owner

@emersion emersion Mar 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of duplicating these statements, can we maybe use defer here?

prevLineLimit := c.lineLimitReader.LineLimit
c.lineLimitReader.LineLimit = 0
defer func() {
	c.lineLimitReader.LineLimit = prevLineLimit
}()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try it. But it not worked as expected.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, what went wrong exactly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just don't work. If i'm add defer and first chunk handle return error TooLongLine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, what went wrong exactly?

I found problem. This code

defer func() {
	c.lineLimitReader.LineLimit = prevLineLimit
}()

Run go routine and this can run defer and next handle at same time. I fix that with this code

	recover := func() {
		c.lineLimitReader.LineLimit = prevLineLimit
	}
	defer recover()

this fix problem same time running defer goroutine and next handleBdat

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand why it doesn't work without the intermediary variable. It definitely should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It worked. But defer run in goroutine. This goroutine code

func() {
	c.lineLimitReader.LineLimit = prevLineLimit
}()

And some time next handleBdat running before running defer. After this when we try copy chunk

 io.Copy(c.bdatPipe, chunk)

Read return ErrTooLongLine

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer doesn't run in a separate goroutine...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey i'm research problem deeper and create new fix. First we should't restore limit before got BDAT LAST command. It's broke In some cases and return error ToooLongTime. For example read next BDAT command. I'm just put restore lineLimit in last condition and this fix problem.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you're right, if the BDAT transfer is still ongoing, we shouldn't restore the limit.

return
}

Expand All @@ -775,13 +780,15 @@ func (c *Conn) handleBdat(arg string) {

if err == errPanic {
c.Close()
c.lineLimitReader.LineLimit = prevLineLimit
return
}

c.reset()
} else {
c.WriteResponse(250, EnhancedCode{2, 0, 0}, "Continue")
}
c.lineLimitReader.LineLimit = prevLineLimit
}

// ErrDataReset is returned by Reader pased to Data function if client does not
Expand Down
2 changes: 1 addition & 1 deletion lengthlimit_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type lineLimitReader struct {
}

func (r *lineLimitReader) Read(b []byte) (int, error) {
if r.curLineLength > r.LineLimit {
if r.curLineLength > r.LineLimit && r.LineLimit > 0 {
return 0, ErrTooLongLine
}

Expand Down