Skip to content

Commit b0385d2

Browse files
committed
Cleanup code
1 parent 0252df0 commit b0385d2

37 files changed

+1189
-1658
lines changed

.github/workflows/naive-build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
workflow_dispatch:
55
inputs:
66
version:
7-
description: 'Release version (e.g., v1.0.0). Creates GitHub Release draft when provided.'
7+
description: 'Release tag'
88
type: string
99
required: false
1010
push:
@@ -29,7 +29,7 @@ jobs:
2929
submodules: 'recursive'
3030
- uses: actions/setup-go@v5
3131
with:
32-
go-version: ^1.22
32+
go-version: ^1.25
3333
- name: Get naiveproxy commit
3434
id: naive
3535
run: echo "commit=$(git -C naiveproxy rev-parse HEAD)" >> $GITHUB_OUTPUT

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ linters:
66
enable:
77
- govet
88
- ineffassign
9-
- paralleltest
109
- staticcheck
1110
settings:
1211
staticcheck:

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ build:
55
go run -v ./cmd/build-naive build $(TARGET_FLAG)
66
go run -v ./cmd/build-naive package --local $(TARGET_FLAG)
77

8+
generate_net_errors:
9+
go run ./cmd/build-naive generate-net-errors
10+
811
test: make
912
go test -v .
1013

bidirectional_conn.go

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import (
99
"time"
1010
)
1111

12-
// BidirectionalConn is a wrapper from BidirectionalStream to net.Conn
1312
type BidirectionalConn struct {
14-
stream BidirectionalStream
15-
cancelOnce sync.Once // Ensures Cancel is called at most once
16-
destroyOnce sync.Once // Ensures Destroy is called at most once
13+
stream BidirectionalStream
14+
cancelOnce sync.Once // Ensures Cancel is called at most once
15+
destroyOnce sync.Once // Ensures Destroy is called at most once
1716
readWaitHeaders bool
1817
writeWaitHeaders bool
1918
access sync.Mutex
@@ -25,26 +24,14 @@ type BidirectionalConn struct {
2524
read chan int
2625
write chan struct{}
2726
headers map[string]string
28-
29-
// readSemaphore/writeSemaphore ensure that only one Read/Write operation is
30-
// in-flight at a time. This is required for Cronet and prevents reuse of
31-
// internal buffers while Cronet still holds pointers to them.
32-
readSemaphore chan struct{}
33-
writeSemaphore chan struct{}
34-
35-
// Cronet holds pointers to the Read/Write buffers asynchronously until the
36-
// corresponding callback fires. Never pass caller-provided buffers directly,
37-
// as they might reside on the Go stack and be moved during stack growth.
38-
readBuffer []byte
39-
writeBuffer []byte
40-
41-
// Buffer safety: when Read/Write return due to close/done, Cronet may
42-
// still hold the buffer. These channels are closed by callbacks to signal
43-
// it's safe to return. sync.Once ensures close happens exactly once.
44-
readDone chan struct{}
45-
writeDone chan struct{}
46-
readDoneOnce sync.Once
47-
writeDoneOnce sync.Once
27+
readSemaphore chan struct{}
28+
writeSemaphore chan struct{}
29+
readBuffer []byte
30+
writeBuffer []byte
31+
readDone chan struct{}
32+
writeDone chan struct{}
33+
readDoneOnce sync.Once
34+
writeDoneOnce sync.Once
4835
}
4936

5037
func (e StreamEngine) CreateConn(readWaitHeaders bool, writeWaitHeaders bool) *BidirectionalConn {
@@ -84,7 +71,6 @@ func (c *BidirectionalConn) Start(method string, url string, headers map[string]
8471
return nil
8572
}
8673

87-
// Read implements io.Reader
8874
func (c *BidirectionalConn) Read(p []byte) (n int, err error) {
8975
select {
9076
case <-c.close:
@@ -151,18 +137,14 @@ func (c *BidirectionalConn) Read(p []byte) (n int, err error) {
151137
}
152138
return bytesRead, nil
153139
case <-c.done:
154-
// Wait for Cronet to finish using the buffer before returning.
155-
// Callbacks will close readDone when done.
156140
<-c.readDone
157141
return 0, c.err
158142
case <-c.close:
159-
// Close() was called. Wait for OnCanceled to signal buffer safety.
160143
<-c.readDone
161144
return 0, net.ErrClosed
162145
}
163146
}
164147

165-
// Write implements io.Writer
166148
func (c *BidirectionalConn) Write(p []byte) (n int, err error) {
167149
select {
168150
case <-c.close:
@@ -224,28 +206,22 @@ func (c *BidirectionalConn) Write(p []byte) (n int, err error) {
224206
case <-c.write:
225207
return len(p), nil
226208
case <-c.done:
227-
// Wait for Cronet to finish using the buffer before returning.
228-
// Callbacks will close writeDone when done.
229209
<-c.writeDone
230210
return 0, c.err
231211
case <-c.close:
232-
// Close() was called. Wait for OnCanceled to signal buffer safety.
233212
<-c.writeDone
234213
return 0, net.ErrClosed
235214
}
236215
}
237216

238-
// Done implements context.Context
239217
func (c *BidirectionalConn) Done() <-chan struct{} {
240218
return c.done
241219
}
242220

243-
// Err implements context.Context
244221
func (c *BidirectionalConn) Err() error {
245222
return c.err
246223
}
247224

248-
// Close implements io.Closer
249225
func (c *BidirectionalConn) Close() error {
250226
c.access.Lock()
251227

@@ -276,27 +252,22 @@ func (c *BidirectionalConn) signalWriteDone() {
276252
c.writeDoneOnce.Do(func() { close(c.writeDone) })
277253
}
278254

279-
// LocalAddr implements net.Conn
280255
func (c *BidirectionalConn) LocalAddr() net.Addr {
281256
return nil
282257
}
283258

284-
// RemoteAddr implements net.Conn
285259
func (c *BidirectionalConn) RemoteAddr() net.Addr {
286260
return nil
287261
}
288262

289-
// SetDeadline implements net.Conn
290263
func (c *BidirectionalConn) SetDeadline(t time.Time) error {
291264
return os.ErrInvalid
292265
}
293266

294-
// SetReadDeadline implements net.Conn
295267
func (c *BidirectionalConn) SetReadDeadline(t time.Time) error {
296268
return os.ErrInvalid
297269
}
298270

299-
// SetWriteDeadline implements net.Conn
300271
func (c *BidirectionalConn) SetWriteDeadline(t time.Time) error {
301272
return os.ErrInvalid
302273
}

bidirectional_stream_cgo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ func (c BidirectionalStream) Start(method string, url string, headers map[string
8181
if headerLen > 0 {
8282
cHeadersPtr := C.malloc(C.size_t(int(C.sizeof_struct_bidirectional_stream_header) * headerLen))
8383
defer C.free(cHeadersPtr)
84-
var cType *C.bidirectional_stream_header
85-
cType = (*C.bidirectional_stream_header)(cHeadersPtr)
84+
cType := (*C.bidirectional_stream_header)(cHeadersPtr)
8685
cHeaders := unsafe.Slice(cType, headerLen)
8786
var index int
8887
for key, value := range headers {

bidirectional_stream_impl_cgo.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func cBidirectionalStreamToGo(stream *C.bidirectional_stream) BidirectionalStrea
4545
func cronetBidirectionalStreamOnStreamReady(stream *C.bidirectional_stream) {
4646
callback := instanceOfBidirectionalStream(stream)
4747
if callback == nil {
48-
return // Post-destroy callback, silently ignore
48+
return
4949
}
5050
callback.OnStreamReady(cBidirectionalStreamToGo(stream))
5151
}
@@ -54,7 +54,7 @@ func cronetBidirectionalStreamOnStreamReady(stream *C.bidirectional_stream) {
5454
func cronetBidirectionalStreamOnResponseHeadersReceived(stream *C.bidirectional_stream, headers *C.bidirectional_stream_header_array, negotiatedProtocol *C.char) {
5555
callback := instanceOfBidirectionalStream(stream)
5656
if callback == nil {
57-
return // Post-destroy callback, silently ignore
57+
return
5858
}
5959
var headerMap map[string]string
6060
if headers != nil && headers.count > 0 {
@@ -75,7 +75,7 @@ func cronetBidirectionalStreamOnResponseHeadersReceived(stream *C.bidirectional_
7575
func cronetBidirectionalStreamOnReadCompleted(stream *C.bidirectional_stream, data *C.char, bytesRead C.int) {
7676
callback := instanceOfBidirectionalStream(stream)
7777
if callback == nil {
78-
return // Post-destroy callback, silently ignore
78+
return
7979
}
8080
callback.OnReadCompleted(cBidirectionalStreamToGo(stream), int(bytesRead))
8181
}
@@ -84,7 +84,7 @@ func cronetBidirectionalStreamOnReadCompleted(stream *C.bidirectional_stream, da
8484
func cronetBidirectionalStreamOnWriteCompleted(stream *C.bidirectional_stream, data *C.char) {
8585
callback := instanceOfBidirectionalStream(stream)
8686
if callback == nil {
87-
return // Post-destroy callback, silently ignore
87+
return
8888
}
8989
callback.OnWriteCompleted(cBidirectionalStreamToGo(stream))
9090
}
@@ -93,7 +93,7 @@ func cronetBidirectionalStreamOnWriteCompleted(stream *C.bidirectional_stream, d
9393
func cronetBidirectionalStreamOnResponseTrailersReceived(stream *C.bidirectional_stream, trailers *C.bidirectional_stream_header_array) {
9494
callback := instanceOfBidirectionalStream(stream)
9595
if callback == nil {
96-
return // Post-destroy callback, silently ignore
96+
return
9797
}
9898
var trailersMap map[string]string
9999
if trailers != nil && trailers.count > 0 {
@@ -114,31 +114,28 @@ func cronetBidirectionalStreamOnResponseTrailersReceived(stream *C.bidirectional
114114
func cronetBidirectionalStreamOnSucceed(stream *C.bidirectional_stream) {
115115
callback := instanceOfBidirectionalStream(stream)
116116
if callback == nil {
117-
return // Post-destroy callback, silently ignore
117+
return
118118
}
119119
callback.OnSucceeded(cBidirectionalStreamToGo(stream))
120-
// Terminal callback - safe to cleanup
121120
cleanupBidirectionalStream(uintptr(unsafe.Pointer(stream)))
122121
}
123122

124123
//export cronetBidirectionalStreamOnFailed
125124
func cronetBidirectionalStreamOnFailed(stream *C.bidirectional_stream, netError C.int) {
126125
callback := instanceOfBidirectionalStream(stream)
127126
if callback == nil {
128-
return // Post-destroy callback, silently ignore
127+
return
129128
}
130129
callback.OnFailed(cBidirectionalStreamToGo(stream), int(netError))
131-
// Terminal callback - safe to cleanup
132130
cleanupBidirectionalStream(uintptr(unsafe.Pointer(stream)))
133131
}
134132

135133
//export cronetBidirectionalStreamOnCanceled
136134
func cronetBidirectionalStreamOnCanceled(stream *C.bidirectional_stream) {
137135
callback := instanceOfBidirectionalStream(stream)
138136
if callback == nil {
139-
return // Post-destroy callback, silently ignore
137+
return
140138
}
141139
callback.OnCanceled(cBidirectionalStreamToGo(stream))
142-
// Terminal callback - safe to cleanup
143140
cleanupBidirectionalStream(uintptr(unsafe.Pointer(stream)))
144141
}

bidirectional_stream_impl_purego.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"github.com/ebitengine/purego"
1111
)
1212

13-
// bidirectionalStreamCallbackStruct mirrors the C struct bidirectional_stream_callback.
14-
// It contains 8 function pointers in the same order as the C struct.
1513
type bidirectionalStreamCallbackStruct struct {
1614
onStreamReady uintptr
1715
onResponseHeadersReceived uintptr
@@ -23,14 +21,12 @@ type bidirectionalStreamCallbackStruct struct {
2321
onCanceled uintptr
2422
}
2523

26-
// bidirectionalStreamHeaderArray mirrors the C struct bidirectional_stream_header_array
2724
type bidirectionalStreamHeaderArray struct {
2825
count uintptr
2926
capacity uintptr
3027
headers uintptr
3128
}
3229

33-
// bidirectionalStreamHeader mirrors the C struct bidirectional_stream_header
3430
type bidirectionalStreamHeader struct {
3531
key uintptr // const char*
3632
value uintptr // const char*
@@ -52,7 +48,7 @@ func init() {
5248
func bsOnStreamReadyCallback(stream uintptr) uintptr {
5349
cb := instanceOfBidirectionalStreamCallback(stream)
5450
if cb == nil {
55-
return 0 // Post-destroy callback, silently ignore
51+
return 0
5652
}
5753
cb.OnStreamReady(BidirectionalStream{stream})
5854
return 0
@@ -61,7 +57,7 @@ func bsOnStreamReadyCallback(stream uintptr) uintptr {
6157
func bsOnResponseHeadersReceivedCallback(stream, headers, negotiatedProtocol uintptr) uintptr {
6258
cb := instanceOfBidirectionalStreamCallback(stream)
6359
if cb == nil {
64-
return 0 // Post-destroy callback, silently ignore
60+
return 0
6561
}
6662
headerMap := parseHeaderArray(headers)
6763
cb.OnResponseHeadersReceived(BidirectionalStream{stream}, headerMap, cronet.GoString(negotiatedProtocol))
@@ -71,7 +67,7 @@ func bsOnResponseHeadersReceivedCallback(stream, headers, negotiatedProtocol uin
7167
func bsOnReadCompletedCallback(stream, data uintptr, bytesRead int32) uintptr {
7268
cb := instanceOfBidirectionalStreamCallback(stream)
7369
if cb == nil {
74-
return 0 // Post-destroy callback, silently ignore
70+
return 0
7571
}
7672
cb.OnReadCompleted(BidirectionalStream{stream}, int(bytesRead))
7773
return 0
@@ -80,7 +76,7 @@ func bsOnReadCompletedCallback(stream, data uintptr, bytesRead int32) uintptr {
8076
func bsOnWriteCompletedCallback(stream, data uintptr) uintptr {
8177
cb := instanceOfBidirectionalStreamCallback(stream)
8278
if cb == nil {
83-
return 0 // Post-destroy callback, silently ignore
79+
return 0
8480
}
8581
cb.OnWriteCompleted(BidirectionalStream{stream})
8682
return 0
@@ -89,7 +85,7 @@ func bsOnWriteCompletedCallback(stream, data uintptr) uintptr {
8985
func bsOnResponseTrailersReceivedCallback(stream, trailers uintptr) uintptr {
9086
cb := instanceOfBidirectionalStreamCallback(stream)
9187
if cb == nil {
92-
return 0 // Post-destroy callback, silently ignore
88+
return 0
9389
}
9490
trailerMap := parseHeaderArray(trailers)
9591
cb.OnResponseTrailersReceived(BidirectionalStream{stream}, trailerMap)
@@ -99,37 +95,33 @@ func bsOnResponseTrailersReceivedCallback(stream, trailers uintptr) uintptr {
9995
func bsOnSucceededCallback(stream uintptr) uintptr {
10096
cb := instanceOfBidirectionalStreamCallback(stream)
10197
if cb == nil {
102-
return 0 // Post-destroy callback, silently ignore
98+
return 0
10399
}
104100
cb.OnSucceeded(BidirectionalStream{stream})
105-
// Terminal callback - safe to cleanup
106101
cleanupBidirectionalStream(stream)
107102
return 0
108103
}
109104

110105
func bsOnFailedCallback(stream uintptr, netError int32) uintptr {
111106
cb := instanceOfBidirectionalStreamCallback(stream)
112107
if cb == nil {
113-
return 0 // Post-destroy callback, silently ignore
108+
return 0
114109
}
115110
cb.OnFailed(BidirectionalStream{stream}, int(netError))
116-
// Terminal callback - safe to cleanup
117111
cleanupBidirectionalStream(stream)
118112
return 0
119113
}
120114

121115
func bsOnCanceledCallback(stream uintptr) uintptr {
122116
cb := instanceOfBidirectionalStreamCallback(stream)
123117
if cb == nil {
124-
return 0 // Post-destroy callback, silently ignore
118+
return 0
125119
}
126120
cb.OnCanceled(BidirectionalStream{stream})
127-
// Terminal callback - safe to cleanup
128121
cleanupBidirectionalStream(stream)
129122
return 0
130123
}
131124

132-
// parseHeaderArray parses the bidirectional_stream_header_array pointer into a Go map
133125
func parseHeaderArray(ptr uintptr) map[string]string {
134126
if ptr == 0 {
135127
return nil

bidirectional_stream_map.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import (
55
"sync/atomic"
66
)
77

8-
// bidirectionalStreamEntry holds a callback and its destroyed state.
9-
// The destroyed flag is used to handle async destruction - callbacks
10-
// may be invoked after Destroy() returns due to Cronet's async API.
118
type bidirectionalStreamEntry struct {
129
callback BidirectionalStreamCallback
1310
destroyed atomic.Bool
@@ -22,9 +19,6 @@ func init() {
2219
bidirectionalStreamMap = make(map[uintptr]*bidirectionalStreamEntry)
2320
}
2421

25-
// instanceOfBidirectionalStreamCallback returns the callback for the given stream pointer.
26-
// Returns nil if the stream was destroyed or not found. Callers should silently
27-
// return on nil as post-destroy callbacks are expected async API behavior.
2822
func instanceOfBidirectionalStreamCallback(ptr uintptr) BidirectionalStreamCallback {
2923
bidirectionalStreamAccess.RLock()
3024
defer bidirectionalStreamAccess.RUnlock()
@@ -35,8 +29,6 @@ func instanceOfBidirectionalStreamCallback(ptr uintptr) BidirectionalStreamCallb
3529
return entry.callback
3630
}
3731

38-
// cleanupBidirectionalStream removes the stream entry from the map.
39-
// Should be called from terminal callbacks (OnSucceeded/OnFailed/OnCanceled).
4032
func cleanupBidirectionalStream(ptr uintptr) {
4133
bidirectionalStreamAccess.Lock()
4234
delete(bidirectionalStreamMap, ptr)

0 commit comments

Comments
 (0)