Skip to content

Commit 447084f

Browse files
committed
Fix transactions + race conditions
1 parent 41c35be commit 447084f

File tree

2 files changed

+23
-36
lines changed

2 files changed

+23
-36
lines changed

lib/backend.js

+11-18
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ function Backend({
1111
parsers,
1212
onauth,
1313
onready,
14-
resolve,
15-
reject,
1614
transform,
1715
onnotice,
1816
onnotify
@@ -85,11 +83,6 @@ function Backend({
8583
break
8684
}
8785
}
88-
89-
resolve(backend.query.stream
90-
? backend.query.result.count
91-
: backend.query.result
92-
)
9386
}
9487

9588
function CopyDone() { /* No handling needed */ }
@@ -126,17 +119,17 @@ function Backend({
126119
function CopyData() { /* No handling needed until implemented */ }
127120

128121
function ErrorResponse(x) {
129-
reject(errors.generic(error(x)))
122+
backend.error = errors.generic(error(x))
130123
}
131124

132125
/* c8 ignore next */
133126
function CopyInResponse() {
134-
reject(errors.notSupported('CopyInResponse'))
127+
backend.error = errors.notSupported('CopyInResponse')
135128
}
136129

137130
/* c8 ignore next */
138131
function CopyOutResponse() {
139-
reject(errors.notSupported('CopyOutResponse'))
132+
backend.error = errors.notSupported('CopyOutResponse')
140133
}
141134

142135
/* c8 ignore next */
@@ -160,7 +153,7 @@ function Backend({
160153
try {
161154
type !== 0 && onauth(type, x)
162155
} catch (err) {
163-
reject(err)
156+
backend.error = err
164157
}
165158
}
166159

@@ -171,12 +164,12 @@ function Backend({
171164

172165
/* c8 ignore next */
173166
function PortalSuspended() {
174-
reject(errors.notSupported('PortalSuspended'))
167+
backend.error = errors.notSupported('PortalSuspended')
175168
}
176169

177170
/* c8 ignore next */
178171
function ParameterDescription() {
179-
reject(errors.notSupported('ParameterDescription'))
172+
backend.error = errors.notSupported('ParameterDescription')
180173
}
181174

182175
function RowDescription(x) {
@@ -206,21 +199,21 @@ function Backend({
206199

207200
/* c8 ignore next */
208201
function FunctionCallResponse() {
209-
reject(errors.notSupported('FunctionCallResponse'))
202+
backend.error = errors.notSupported('FunctionCallResponse')
210203
}
211204

212205
/* c8 ignore next */
213206
function NegotiateProtocolVersion() {
214-
reject(errors.notSupported('NegotiateProtocolVersion'))
207+
backend.error = errors.notSupported('NegotiateProtocolVersion')
215208
}
216209

217210
/* c8 ignore next */
218211
function CopyBothResponse() {
219-
reject(errors.notSupported('CopyBothResponse'))
212+
backend.error = errors.notSupported('CopyBothResponse')
220213
}
221214

222-
function ReadyForQuery() {
223-
onready()
215+
function ReadyForQuery(x) {
216+
onready(backend.error)
224217
}
225218
}
226219

lib/connection.js

+12-18
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ function Connection(options = {}) {
4141
onparameter,
4242
transform,
4343
parsers,
44-
resolve,
45-
reject,
4644
onnotify,
4745
onnotice,
4846
onready,
@@ -60,18 +58,6 @@ function Connection(options = {}) {
6058
socket.write(frontend.auth(type, x, options))
6159
}
6260

63-
function resolve(x) {
64-
backend.query.resolve(x)
65-
backend.query = null
66-
timeout && queries.length === 0 && idle()
67-
}
68-
69-
function reject(err) {
70-
backend.query ? backend.query.reject(err) : error(err)
71-
backend.query = null
72-
timeout && queries.length === 0 && idle()
73-
}
74-
7561
function end() {
7662
clearTimeout(timer)
7763
const promise = new Promise((resolve) => {
@@ -138,8 +124,17 @@ function Connection(options = {}) {
138124
timer = setTimeout(socket.end, timeout * 1000)
139125
}
140126

141-
function onready() {
142-
if (!backend.query && queries.length === 0 && ended)
127+
function onready(err) {
128+
err
129+
? (backend.query ? backend.query.reject(err) : error(err))
130+
: (backend.query && backend.query.resolve(backend.query.stream
131+
? backend.query.result.count
132+
: backend.query.result))
133+
134+
backend.error = null
135+
timeout && queries.length === 0 && idle()
136+
137+
if (queries.length === 0 && ended)
143138
return ended()
144139

145140
if (!ready) {
@@ -148,8 +143,7 @@ function Connection(options = {}) {
148143
ready = true
149144
}
150145

151-
if (!backend.query)
152-
backend.query = queries.shift()
146+
backend.query = queries.shift()
153147
}
154148

155149
function data(x) {

0 commit comments

Comments
 (0)