• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lunarmodules / copas / 27209761779

09 Jun 2026 01:32PM UTC coverage: 84.991% (-0.1%) from 85.118%
27209761779

push

github

web-flow
Merge f076b00b3 into 5350f0c11

3 of 6 new or added lines in 1 file covered. (50.0%)

11 existing lines in 5 files now uncovered.

1410 of 1659 relevant lines covered (84.99%)

78807.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.98
/src/copas.lua
1
-------------------------------------------------------------------------------
2
-- Copas - Coroutine Oriented Portable Asynchronous Services
3
--
4
-- A dispatcher based on coroutines that can be used by TCP/IP servers.
5
-- Uses LuaSocket as the interface with the TCP/IP stack.
6
--
7
-- Authors: Andre Carregal, Javier Guerra, and Fabio Mascarenhas
8
-- Contributors: Diego Nehab, Mike Pall, David Burgess, Leonardo Godinho,
9
--               Thomas Harning Jr., and Gary NG
10
--
11
-- Copyright 2005-2013 - Kepler Project (www.keplerproject.org), 2015-2025 Thijs Schreijer
12
--
13
-- $Id: copas.lua,v 1.37 2009/04/07 22:09:52 carregal Exp $
14
-------------------------------------------------------------------------------
15

16
if package.loaded["socket.http"] and (_VERSION=="Lua 5.1") then     -- obsolete: only for Lua 5.1 compatibility
218✔
17
  error("you must require copas before require'ing socket.http")
×
18
end
19
if package.loaded["copas.http"] and (_VERSION=="Lua 5.1") then     -- obsolete: only for Lua 5.1 compatibility
218✔
20
  error("you must require copas before require'ing copas.http")
×
21
end
22

23
-- load either LuaSocket, or LuaSystem
24
-- note: with luasocket we don't use 'sleep' but 'select' with no sockets
25
local socket, system do
218✔
26
  if pcall(require, "socket") then
220✔
27
    -- found LuaSocket
28
    socket = require "socket"
211✔
29
  end
30

31
  -- try LuaSystem as fallback
32
  if pcall(require, "system") then
220✔
33
    system = require "system"
220✔
34
  end
35

36
  if not (socket or system) then
218✔
37
    error("Neither LuaSocket nor LuaSystem found, Copas requires at least one of them")
×
38
  end
39
end
40

41
local binaryheap = require "binaryheap"
218✔
42
local gettime = (socket or system).gettime
218✔
43
local block_sleep = (socket or system).sleep
218✔
44
local ssl -- only loaded upon demand
45

46
local core_timer_thread
47
local WATCH_DOG_TIMEOUT = 120
218✔
48
local UDP_DATAGRAM_MAX = (socket or {})._DATAGRAMSIZE or 8192
218✔
49
local TIMEOUT_PRECISION = 0.1  -- 100ms
218✔
50
local fnil = function() end
298,563✔
51

52

53
local coroutine_create = coroutine.create
218✔
54
local coroutine_running = coroutine.running
218✔
55
local coroutine_yield = coroutine.yield
218✔
56
local coroutine_resume = coroutine.resume
218✔
57
local coroutine_status = coroutine.status
218✔
58

59

60
-- nil-safe versions for pack/unpack
61
local _unpack = unpack or table.unpack
218✔
62
local unpack = function(t, i, j) return _unpack(t, i or 1, j or t.n or #t) end
1,244✔
63
local pack = function(...) return { n = select("#", ...), ...} end
1,750✔
64

65

66
local pcall = pcall
218✔
67
if _VERSION=="Lua 5.1" and not jit then     -- obsolete: only for Lua 5.1 compatibility
218✔
UNCOV
68
  pcall = require("coxpcall").pcall
30✔
UNCOV
69
  coroutine_running = require("coxpcall").running
30✔
70
end
71

72

73
if socket then
218✔
74
  -- Redefines LuaSocket functions with coroutine safe versions (pure Lua)
75
  -- (this allows the use of socket.http from within copas)
76
  local err_mt = {
211✔
77
    __tostring = function (self)
78
      return "Copas 'try' error intermediate table: '"..tostring(self[1].."'")
×
79
    end,
80
  }
81

82
  local function statusHandler(status, ...)
83
    if status then return ... end
98✔
84
    local err = (...)
35✔
85
    if type(err) == "table" and getmetatable(err) == err_mt then
35✔
86
      return nil, err[1]
35✔
87
    else
88
      error(err)
×
89
    end
90
  end
91

92
  function socket.protect(func)
211✔
93
    return function (...)
94
            return statusHandler(pcall(func, ...))
126✔
95
          end
96
  end
97

98
  function socket.newtry(finalizer)
211✔
99
    return function (...)
100
            local status = (...)
1,610✔
101
            if not status then
1,610✔
102
              pcall(finalizer or fnil, select(2, ...))
35✔
103
              error(setmetatable({ (select(2, ...)) }, err_mt), 0)
35✔
104
            end
105
            return ...
1,575✔
106
          end
107
  end
108

109
  socket.try = socket.newtry()
269✔
110
end
111

112

113
-- Setup the Copas meta table to auto-load submodules and define a default method
114
local copas do
218✔
115
  local submodules = { "ftp", "future", "http", "lock", "queue", "semaphore", "smtp", "timer" }
218✔
116
  for i, key in ipairs(submodules) do
1,962✔
117
    submodules[key] = true
1,744✔
118
    submodules[i] = nil
1,744✔
119
  end
120

121
  copas = setmetatable({},{
436✔
122
    __index = function(self, key)
123
      if submodules[key] then
288✔
124
        self[key] = require("copas."..key)
292✔
125
        submodules[key] = nil
288✔
126
        return rawget(self, key)
288✔
127
      end
128
    end,
129
    __call = function(self, ...)
130
      return self.loop(...)
7✔
131
    end,
132
  })
218✔
133
end
134

135

136
-- Meta information is public even if beginning with an "_"
137
copas._COPYRIGHT   = "Copyright (C) 2005-2013 Kepler Project, 2015-2026 Thijs Schreijer"
218✔
138
copas._DESCRIPTION = "Coroutine Oriented Portable Asynchronous Services"
218✔
139
copas._VERSION     = "Copas 4.10.0"
218✔
140

141
-- Close the socket associated with the current connection after the handler finishes
142
copas.autoclose = true
218✔
143

144
-- indicator for the loop running
145
copas.running = false
218✔
146

147
-- gettime method from either LuaSocket or LuaSystem: time in (fractional) seconds, since epoch.
148
copas.gettime = gettime
218✔
149

150
-------------------------------------------------------------------------------
151
-- Object names, to track names of thread/coroutines and sockets
152
-------------------------------------------------------------------------------
153
local object_names = setmetatable({}, {
436✔
154
  __mode = "k",
158✔
155
  __index = function(self, key)
156
    local name = tostring(key)
224✔
157
    if key ~= nil then
224✔
158
      rawset(self, key, name)
224✔
159
    end
160
    return name
224✔
161
  end
162
})
163

164
-------------------------------------------------------------------------------
165
-- Simple set implementation
166
-- adds a FIFO queue for each socket in the set
167
-------------------------------------------------------------------------------
168

169
local function newsocketset()
170
  local set = {}
654✔
171

172
  do  -- set implementation
173
    local reverse = {}
654✔
174

175
    -- Adds a socket to the set, does nothing if it exists
176
    -- @return skt if added, or nil if it existed
177
    function set:insert(skt)
654✔
178
      if not reverse[skt] then
1,572✔
179
        self[#self + 1] = skt
1,572✔
180
        reverse[skt] = #self
1,572✔
181
        return skt
1,572✔
182
      end
183
    end
184

185
    -- Removes socket from the set, does nothing if not found
186
    -- @return skt if removed, or nil if it wasn't in the set
187
    function set:remove(skt)
654✔
188
      local index = reverse[skt]
2,188✔
189
      if index then
2,188✔
190
        reverse[skt] = nil
1,565✔
191
        local top = self[#self]
1,565✔
192
        self[#self] = nil
1,565✔
193
        if top ~= skt then
1,565✔
194
          reverse[top] = index
203✔
195
          self[index] = top
203✔
196
        end
197
        return skt
1,565✔
198
      end
199
    end
200

201
  end
202

203
  do  -- queues implementation
204
    local fifo_queues = setmetatable({},{
1,308✔
205
      __mode = "k",                 -- auto collect queue if socket is gone
474✔
206
      __index = function(self, skt) -- auto create fifo queue if not found
207
        local newfifo = {}
595✔
208
        self[skt] = newfifo
595✔
209
        return newfifo
595✔
210
      end,
211
    })
212

213
    -- pushes an item in the fifo queue for the socket.
214
    function set:push(skt, itm)
654✔
215
      local queue = fifo_queues[skt]
1,460✔
216
      queue[#queue + 1] = itm
1,460✔
217
    end
218

219
    -- pops an item from the fifo queue for the socket
220
    function set:pop(skt)
654✔
221
      local queue = fifo_queues[skt]
1,369✔
222
      return table.remove(queue, 1)
1,369✔
223
    end
224

225
  end
226

227
  return set
654✔
228
end
229

230

231

232
-- Threads immediately resumable
233
local _resumable = {} do
218✔
234
  local resumelist = {}
218✔
235

236
  function _resumable:push(co)
218✔
237
    resumelist[#resumelist + 1] = co
298,179✔
238
  end
239

240
  function _resumable:clear_resumelist()
218✔
241
    local lst = resumelist
287,370✔
242
    resumelist = {}
287,370✔
243
    return lst
287,370✔
244
  end
245

246
  function _resumable:done()
218✔
247
    return resumelist[1] == nil
291,332✔
248
  end
249

250
  function _resumable:count()
218✔
251
    return #resumelist + #_resumable
×
252
  end
253

254
end
255

256

257

258
-- Similar to the socket set above, but tailored for the use of
259
-- sleeping threads
260
local _sleeping = {} do
218✔
261

262
  local heap = binaryheap.minUnique()
218✔
263
  local lethargy = setmetatable({}, { __mode = "k" }) -- list of coroutines sleeping without a wakeup time
218✔
264

265

266
  -- Required base implementation
267
  -----------------------------------------
268
  _sleeping.insert = fnil
218✔
269
  _sleeping.remove = fnil
218✔
270

271
  -- push a new timer on the heap
272
  function _sleeping:push(sleeptime, co)
218✔
273
    if sleeptime < 0 then
298,338✔
274
      lethargy[co] = true
3,759✔
275
    elseif sleeptime == 0 then
294,579✔
276
      _resumable:push(co)
418,536✔
277
    else
278
      heap:insert(gettime() + sleeptime, co)
8,581✔
279
    end
280
  end
281

282
  -- find the thread that should wake up to the time, if any
283
  function _sleeping:pop(time)
218✔
284
    if time < (heap:peekValue() or math.huge) then
431,060✔
285
      return
287,370✔
286
    end
287
    return heap:pop()
8,338✔
288
  end
289

290
  -- additional methods for time management
291
  -----------------------------------------
292
  function _sleeping:getnext()  -- returns delay until next sleep expires, or nil if there is none
218✔
293
    local t = heap:peekValue()
7,040✔
294
    if t then
7,040✔
295
      -- never report less than 0, because select() might block
296
      return math.max(t - gettime(), 0)
7,040✔
297
    end
298
  end
299

300
  function _sleeping:wakeup(co)
218✔
301
    if lethargy[co] then
3,773✔
302
      lethargy[co] = nil
3,738✔
303
      _resumable:push(co)
3,738✔
304
      return
3,738✔
305
    end
306
    if heap:remove(co) then
45✔
307
      _resumable:push(co)
14✔
308
    end
309
  end
310

311
  function _sleeping:cancel(co)
218✔
312
    lethargy[co] = nil
63✔
313
    heap:remove(co)
63✔
314
  end
315

316
  function _sleeping:cancelall()
218✔
317
    while heap:size() > 0 do heap:pop() end
×
318
    heap:insert(gettime() + TIMEOUT_PRECISION, core_timer_thread)
×
319
    -- lethargy is weak; copas's idle GC sweeps will clean it within a few steps
320
  end
321

322
  -- @param tos number of timeouts running
323
  function _sleeping:done(tos)
218✔
324
    -- return true if we have nothing more to do
325
    -- the timeout task doesn't qualify as work (fallbacks only),
326
    -- the lethargy also doesn't qualify as work ('dead' tasks),
327
    -- but the combination of a timeout + a lethargy can be work
328
    return heap:size() == 1       -- 1 means only the timeout-timer task is running
4,038✔
329
           and not (tos > 0 and next(lethargy))
3,150✔
330
  end
331

332
  -- gets number of threads in binaryheap and lethargy
333
  function _sleeping:status()
218✔
334
    local c = 0
×
335
    for _ in pairs(lethargy) do c = c + 1 end
×
336

337
    return heap:size(), c
×
338
  end
339

340
end   -- _sleeping
341

342

343

344
-------------------------------------------------------------------------------
345
-- Tracking coroutines and sockets
346
-------------------------------------------------------------------------------
347

348
local _servers = newsocketset() -- servers being handled
218✔
349
local _threads = setmetatable({}, {__mode = "k"})  -- registered threads added with addthread()
218✔
350
local _canceled = setmetatable({}, {__mode = "k"}) -- threads that are canceled and pending removal
218✔
351
local _autoclose = setmetatable({}, {__mode = "kv"}) -- sockets (value) to close when a thread (key) exits
218✔
352
local _autoclose_r = setmetatable({}, {__mode = "kv"}) -- reverse: sockets (key) to close when a thread (value) exits
218✔
353

354

355
-- for each socket we log the last read and last write times to enable the
356
-- watchdog to follow up if it takes too long.
357
-- tables contain the time, indexed by the socket
358
local _reading_log = {}
218✔
359
local _writing_log = {}
218✔
360

361
local _closed = {} -- track sockets that have been closed (list/array)
218✔
362

363
local _reading = newsocketset() -- sockets currently being read
218✔
364
local _writing = newsocketset() -- sockets currently being written
218✔
365
local _isSocketTimeout = { -- set of errors indicating a socket-timeout
218✔
366
  ["timeout"] = true,      -- default LuaSocket timeout
158✔
367
  ["wantread"] = true,     -- LuaSec specific timeout
158✔
368
  ["wantwrite"] = true,    -- LuaSec specific timeout
158✔
369
}
370

371
-------------------------------------------------------------------------------
372
-- Coroutine based socket timeouts.
373
-------------------------------------------------------------------------------
374
local user_timeouts_connect
375
local user_timeouts_send
376
local user_timeouts_receive
377
do
378
  local timeout_mt = {
218✔
379
    __mode = "k",
158✔
380
    __index = function(self, skt)
381
      -- if there is no timeout found, we insert one automatically, to block forever
382
      self[skt] = math.huge
504✔
383
      return self[skt]
504✔
384
    end,
385
  }
386

387
  user_timeouts_connect = setmetatable({}, timeout_mt)
218✔
388
  user_timeouts_send = setmetatable({}, timeout_mt)
218✔
389
  user_timeouts_receive = setmetatable({}, timeout_mt)
218✔
390
end
391

392
local useSocketTimeoutErrors = setmetatable({},{ __mode = "k" })
218✔
393

394

395
-- sto = socket-time-out
396
local sto_timeout, sto_timed_out, sto_change_queue, sto_error do
218✔
397

398
  local socket_register = setmetatable({}, { __mode = "k" })    -- socket by coroutine
218✔
399
  local operation_register = setmetatable({}, { __mode = "k" }) -- operation "read"/"write" by coroutine
218✔
400
  local timeout_flags = setmetatable({}, { __mode = "k" })      -- true if timedout, by coroutine
218✔
401

402

403
  local function socket_callback(co)
404
    local skt = socket_register[co]
91✔
405
    local queue = operation_register[co]
91✔
406

407
    -- flag the timeout and resume the coroutine
408
    timeout_flags[co] = true
91✔
409
    _resumable:push(co)
91✔
410

411
    -- clear the socket from the current queue
412
    if queue == "read" then
91✔
413
      _reading:remove(skt)
99✔
414
    elseif queue == "write" then
14✔
415
      _writing:remove(skt)
18✔
416
    else
417
      error("bad queue name; expected 'read'/'write', got: "..tostring(queue))
×
418
    end
419
  end
420

421

422
  -- Sets a socket timeout.
423
  -- Calling it as `sto_timeout()` will cancel the timeout.
424
  -- @param queue (string) the queue the socket is currently in, must be either "read" or "write"
425
  -- @param skt (socket) the socket on which to operate
426
  -- @param use_connect_to (bool) timeout to use is determined based on queue (read/write) or if this
427
  -- is truthy, it is the connect timeout.
428
  -- @return true
429
  function sto_timeout(skt, queue, use_connect_to)
188✔
430
    local co = coroutine_running()
5,376,312✔
431
    socket_register[co] = skt
5,376,312✔
432
    operation_register[co] = queue
5,376,312✔
433
    timeout_flags[co] = nil
5,376,312✔
434
    if skt then
5,376,312✔
435
      local to = (use_connect_to and user_timeouts_connect[skt]) or
2,688,178✔
436
                 (queue == "read" and user_timeouts_receive[skt]) or
2,687,789✔
437
                 user_timeouts_send[skt]
17,759✔
438
      copas.timeout(to, socket_callback)
3,943,347✔
439
    else
440
      copas.timeout(0)
2,688,156✔
441
    end
442
    return true
5,376,312✔
443
  end
444

445

446
  -- Changes the timeout to a different queue (read/write).
447
  -- Only usefull with ssl-handshakes and "wantread", "wantwrite" errors, when
448
  -- the queue has to be changed, so the timeout handler knows where to find the socket.
449
  -- @param queue (string) the new queue the socket is in, must be either "read" or "write"
450
  -- @return true
451
  function sto_change_queue(queue)
188✔
452
    operation_register[coroutine_running()] = queue
1,194✔
453
    return true
1,194✔
454
  end
455

456

457
  -- Responds with `true` if the operation timed-out.
458
  function sto_timed_out()
188✔
459
    return timeout_flags[coroutine_running()]
1,551✔
460
  end
461

462

463
  -- Returns the proper timeout error
464
  function sto_error(err)
188✔
465
    return useSocketTimeoutErrors[coroutine_running()] and err or "timeout"
91✔
466
  end
467
end
468

469

470

471
-------------------------------------------------------------------------------
472
-- Coroutine based socket I/O functions.
473
-------------------------------------------------------------------------------
474

475
-- Returns "tcp"" for plain TCP and "ssl" for ssl-wrapped sockets, so truthy
476
-- for tcp based, and falsy for udp based.
477
local isTCP do
218✔
478
  local lookup = {
218✔
479
    tcp = "tcp",
158✔
480
    SSL = "ssl",
158✔
481
  }
482

483
  function isTCP(socket)
188✔
484
    return lookup[tostring(socket):sub(1,3)]
972✔
485
  end
486
end
487

488
function copas.close(skt, ...)
218✔
489
  _closed[#_closed+1] = skt
259✔
490
  return skt:close(...)
259✔
491
end
492

493

494

495
-- nil or negative is indefinitly
496
function copas.settimeout(skt, timeout)
218✔
497
  timeout = timeout or -1
252✔
498
  if type(timeout) ~= "number" then
252✔
499
    return nil, "timeout must be 'nil' or a number"
21✔
500
  end
501

502
  return copas.settimeouts(skt, timeout, timeout, timeout)
231✔
503
end
504

505
-- negative is indefinitly, nil means do not change
506
function copas.settimeouts(skt, connect, send, read)
218✔
507

508
  if connect ~= nil and type(connect) ~= "number" then
504✔
509
    return nil, "connect timeout must be 'nil' or a number"
×
510
  end
511
  if connect then
504✔
512
    if connect < 0 then
504✔
513
      connect = nil
×
514
    end
515
    user_timeouts_connect[skt] = connect
504✔
516
  end
517

518

519
  if send ~= nil and type(send) ~= "number" then
504✔
520
    return nil, "send timeout must be 'nil' or a number"
×
521
  end
522
  if send then
504✔
523
    if send < 0 then
504✔
524
      send = nil
×
525
    end
526
    user_timeouts_send[skt] = send
504✔
527
  end
528

529

530
  if read ~= nil and type(read) ~= "number" then
504✔
531
    return nil, "read timeout must be 'nil' or a number"
×
532
  end
533
  if read then
504✔
534
    if read < 0 then
504✔
535
      read = nil
×
536
    end
537
    user_timeouts_receive[skt] = read
504✔
538
  end
539

540

541
  return true
504✔
542
end
543

544
-- reads a pattern from a client and yields to the reading set on timeouts
545
-- UDP: a UDP socket expects a second argument to be a number, so it MUST
546
-- be provided as the 'pattern' below defaults to a string. Will throw a
547
-- 'bad argument' error if omitted.
548
function copas.receive(client, pattern, part)
218✔
549
  local s, err
550
  pattern = pattern or "*l"
2,669,984✔
551
  local current_log = _reading_log
2,669,984✔
552
  sto_timeout(client, "read")
2,669,984✔
553

554
  repeat
555
    s, err, part = client:receive(pattern, part)
2,670,741✔
556

557
    -- guarantees that high throughput doesn't take other threads to starvation
558
    if (math.random(100) > 90) then
2,670,741✔
559
      copas.pause()
267,229✔
560
    end
561

562
    if s then
2,670,741✔
563
      current_log[client] = nil
2,669,858✔
564
      sto_timeout()
2,669,858✔
565
      return s, err, part
2,669,858✔
566

567
    elseif not _isSocketTimeout[err] then
883✔
568
      current_log[client] = nil
56✔
569
      sto_timeout()
56✔
570
      return s, err, part
56✔
571

572
    elseif sto_timed_out() then
1,095✔
573
      current_log[client] = nil
70✔
574
      sto_timeout()
70✔
575
      return nil, sto_error(err), part
90✔
576
    end
577

578
    if err == "wantwrite" then -- wantwrite may be returned during SSL renegotiations
757✔
579
      current_log = _writing_log
×
580
      current_log[client] = gettime()
×
581
      sto_change_queue("write")
×
582
      coroutine_yield(client, _writing)
×
583
    else
584
      current_log = _reading_log
757✔
585
      current_log[client] = gettime()
757✔
586
      sto_change_queue("read")
757✔
587
      coroutine_yield(client, _reading)
757✔
588
    end
589
  until false
757✔
590
end
591

592
-- receives data from a client over UDP. Not available for TCP.
593
-- (this is a copy of receive() method, adapted for receivefrom() use)
594
function copas.receivefrom(client, size)
218✔
595
  local s, err, port
596
  size = size or UDP_DATAGRAM_MAX
28✔
597
  sto_timeout(client, "read")
28✔
598

599
  repeat
600
    s, err, port = client:receivefrom(size) -- upon success err holds ip address
56✔
601

602
    -- garantees that high throughput doesn't take other threads to starvation
603
    if (math.random(100) > 90) then
56✔
604
      copas.pause()
9✔
605
    end
606

607
    if s then
56✔
608
      _reading_log[client] = nil
21✔
609
      sto_timeout()
21✔
610
      return s, err, port
21✔
611

612
    elseif err ~= "timeout" then
35✔
613
      _reading_log[client] = nil
×
614
      sto_timeout()
×
615
      return s, err, port
×
616

617
    elseif sto_timed_out() then
45✔
618
      _reading_log[client] = nil
7✔
619
      sto_timeout()
7✔
620
      return nil, sto_error(err), port
9✔
621
    end
622

623
    _reading_log[client] = gettime()
28✔
624
    coroutine_yield(client, _reading)
28✔
625
  until false
28✔
626
end
627

628
-- same as above but with special treatment when reading chunks,
629
-- unblocks on any data received.
630
function copas.receivepartial(client, pattern, part)
218✔
631
  local s, err
632
  pattern = pattern or "*l"
14✔
633
  local orig_size = #(part or "")
14✔
634
  local current_log = _reading_log
14✔
635
  sto_timeout(client, "read")
14✔
636

637
  repeat
638
    s, err, part = client:receive(pattern, part)
21✔
639

640
    -- guarantees that high throughput doesn't take other threads to starvation
641
    if (math.random(100) > 90) then
21✔
UNCOV
642
      copas.pause()
1✔
643
    end
644

645
    if s or (type(part) == "string" and #part > orig_size) then
21✔
646
      current_log[client] = nil
14✔
647
      sto_timeout()
14✔
648
      return s, err, part
14✔
649

650
    elseif not _isSocketTimeout[err] then
7✔
651
      current_log[client] = nil
×
652
      sto_timeout()
×
653
      return s, err, part
×
654

655
    elseif sto_timed_out() then
9✔
656
      current_log[client] = nil
×
NEW
657
      sto_timeout()
×
UNCOV
658
      return nil, sto_error(err), part
×
659
    end
660

661
    if err == "wantwrite" then
7✔
662
      current_log = _writing_log
×
663
      current_log[client] = gettime()
×
664
      sto_change_queue("write")
×
665
      coroutine_yield(client, _writing)
×
666
    else
667
      current_log = _reading_log
7✔
668
      current_log[client] = gettime()
7✔
669
      sto_change_queue("read")
7✔
670
      coroutine_yield(client, _reading)
7✔
671
    end
672
  until false
7✔
673
end
674
copas.receivePartial = copas.receivepartial  -- compat: receivePartial is deprecated
218✔
675

676
-- sends data to a client. The operation is buffered and
677
-- yields to the writing set on timeouts
678
-- Note: from and to parameters will be ignored by/for UDP sockets
679
function copas.send(client, data, from, to)
218✔
680
  local s, err
681
  from = from or 1
17,759✔
682
  local lastIndex = from - 1
17,759✔
683
  local current_log = _writing_log
17,759✔
684
  sto_timeout(client, "write")
17,759✔
685

686
  repeat
687
    s, err, lastIndex = client:send(data, lastIndex + 1, to)
17,971✔
688

689
    -- guarantees that high throughput doesn't take other threads to starvation
690
    if (math.random(100) > 90) then
17,971✔
691
      copas.pause()
1,748✔
692
    end
693

694
    if s then
17,971✔
695
      current_log[client] = nil
17,731✔
696
      sto_timeout()
17,731✔
697
      return s, err, lastIndex
17,731✔
698

699
    elseif not _isSocketTimeout[err] then
240✔
700
      current_log[client] = nil
28✔
701
      sto_timeout()
28✔
702
      return s, err, lastIndex
28✔
703

704
    elseif sto_timed_out() then
274✔
705
      current_log[client] = nil
×
NEW
706
      sto_timeout()
×
UNCOV
707
      return nil, sto_error(err), lastIndex
×
708
    end
709

710
    if err == "wantread" then
212✔
711
      current_log = _reading_log
×
712
      current_log[client] = gettime()
×
713
      sto_change_queue("read")
×
714
      coroutine_yield(client, _reading)
×
715
    else
716
      current_log = _writing_log
212✔
717
      current_log[client] = gettime()
212✔
718
      sto_change_queue("write")
212✔
719
      coroutine_yield(client, _writing)
212✔
720
    end
721
  until false
212✔
722
end
723

724
function copas.sendto(client, data, ip, port)
218✔
725
  -- deprecated; for backward compatibility only, since UDP doesn't block on sending
726
  return client:sendto(data, ip, port)
×
727
end
728

729
-- waits until connection is completed
730
function copas.connect(skt, host, port)
218✔
731
  skt:settimeout(0)
249✔
732
  local ret, err, tried_more_than_once
733
  sto_timeout(skt, "write", true)
245✔
734

735
  repeat
736
    ret, err = skt:connect(host, port)
499✔
737

738
    -- non-blocking connect on Windows results in error "Operation already
739
    -- in progress" to indicate that it is completing the request async. So essentially
740
    -- it is the same as "timeout"
741
    if ret or (err ~= "timeout" and err ~= "Operation already in progress") then
483✔
742
      _writing_log[skt] = nil
231✔
743
      sto_timeout()
231✔
744
      -- Once the async connect completes, Windows returns the error "already connected"
745
      -- to indicate it is done, so that error should be ignored. Except when it is the
746
      -- first call to connect, then it was already connected to something else and the
747
      -- error should be returned
748
      if (not ret) and (err == "already connected" and tried_more_than_once) then
231✔
749
        return 1
×
750
      end
751
      return ret, err
231✔
752

753
    elseif sto_timed_out() then
324✔
754
      _writing_log[skt] = nil
14✔
755
      sto_timeout()
14✔
756
      return nil, sto_error(err)
18✔
757
    end
758

759
    tried_more_than_once = tried_more_than_once or true
238✔
760
    _writing_log[skt] = gettime()
238✔
761
    coroutine_yield(skt, _writing)
238✔
762
  until false
238✔
763
end
764

765

766
-- Wraps a tcp socket in an ssl socket and configures it. If the socket was
767
-- already wrapped, it does nothing and returns the socket.
768
-- @param wrap_params the parameters for the ssl-context
769
-- @return wrapped socket, or throws an error
770
local function ssl_wrap(skt, wrap_params)
771
  if isTCP(skt) == "ssl" then return skt end -- was already wrapped
288✔
772
  if not wrap_params then
126✔
773
    error("cannot wrap socket into a secure socket (using 'ssl.wrap()') without parameters/context")
×
774
  end
775

776
  ssl = ssl or require("ssl")
126✔
777
  local nskt = assert(ssl.wrap(skt, wrap_params)) -- assert, because we do not want to silently ignore this one!!
162✔
778

779
  nskt:settimeout(0)  -- non-blocking on the ssl-socket
126✔
780
  copas.settimeouts(nskt, user_timeouts_connect[skt],
252✔
781
    user_timeouts_send[skt], user_timeouts_receive[skt]) -- copy copas user-timeout to newly wrapped one
134✔
782

783
  local co = _autoclose_r[skt]
126✔
784
  if co then
126✔
785
    -- socket registered for autoclose, move registration to wrapped one
786
    _autoclose[co] = nskt
28✔
787
    _autoclose_r[skt] = nil
28✔
788
    _autoclose_r[nskt] = co
28✔
789
  end
790

791
  local sock_name = object_names[skt]
126✔
792
  if sock_name ~= tostring(skt) then
126✔
793
    -- socket had a custom name, so copy it over
794
    object_names[nskt] = sock_name
42✔
795
  end
796
  return nskt
126✔
797
end
798

799

800
-- For each luasec method we have a subtable, allows for future extension.
801
-- Required structure:
802
-- {
803
--   wrap = ... -- parameter to 'wrap()'; the ssl parameter table, or the context object
804
--   sni = {                  -- parameters to 'sni()'
805
--     names = string | table -- 1st parameter
806
--     strict = bool          -- 2nd parameter
807
--   }
808
-- }
809
local function normalize_sslt(sslt)
810
  local t = type(sslt)
392✔
811
  local r = setmetatable({}, {
784✔
812
    __index = function(self, key)
813
      -- a bug if this happens, here as a sanity check, just being careful since
814
      -- this is security stuff
815
      error("accessing unknown 'ssl_params' table key: "..tostring(key))
×
816
    end,
817
  })
818
  if t == "nil" then
392✔
819
    r.wrap = false
266✔
820
    r.sni = false
266✔
821

822
  elseif t == "table" then
126✔
823
    if sslt.mode or sslt.protocol then
126✔
824
      -- has the mandatory fields for the ssl-params table for handshake
825
      -- backward compatibility
826
      r.wrap = sslt
28✔
827
      r.sni = false
28✔
828
    else
829
      -- has the target definition, copy our known keys
830
      r.wrap = sslt.wrap or false -- 'or false' because we do not want nils
98✔
831
      r.sni = sslt.sni or false -- 'or false' because we do not want nils
98✔
832
    end
833

834
  elseif t == "userdata" then
×
835
    -- it's an ssl-context object for the handshake
836
    -- backward compatibility
837
    r.wrap = sslt
×
838
    r.sni = false
×
839

840
  else
841
    error("ssl parameters; did not expect type "..tostring(sslt))
×
842
  end
843

844
  return r
392✔
845
end
846

847

848
---
849
-- Peforms an (async) ssl handshake on a connected TCP client socket.
850
-- NOTE: if not ssl-wrapped already, then replace all previous socket references, with the returned new ssl wrapped socket
851
-- Throws error and does not return nil+error, as that might silently fail
852
-- in code like this;
853
--   copas.addserver(s1, function(skt)
854
--       skt = copas.wrap(skt, sparams)
855
--       skt:dohandshake()   --> without explicit error checking, this fails silently and
856
--       skt:send(body)      --> continues unencrypted
857
-- @param skt Regular LuaSocket CLIENT socket object
858
-- @param wrap_params Table with ssl parameters
859
-- @return wrapped ssl socket, or throws an error
860
function copas.dohandshake(skt, wrap_params)
218✔
861
  ssl = ssl or require("ssl")
126✔
862

863
  local nskt = ssl_wrap(skt, wrap_params)
126✔
864

865
  sto_timeout(nskt, "write", true)
126✔
866
  local queue
867

868
  repeat
869
    local success, err = nskt:dohandshake()
344✔
870

871
    if success then
344✔
872
      sto_timeout()
112✔
873
      return nskt
112✔
874

875
    elseif not _isSocketTimeout[err] then
232✔
876
      sto_timeout()
14✔
877
      error("TLS/SSL handshake failed: " .. tostring(err))
14✔
878

879
    elseif sto_timed_out() then
281✔
NEW
880
      sto_timeout()
×
UNCOV
881
      return nil, sto_error(err)
×
882

883
    elseif err == "wantwrite" then
218✔
884
      sto_change_queue("write")
×
885
      queue = _writing
×
886

887
    elseif err == "wantread" then
218✔
888
      sto_change_queue("read")
218✔
889
      queue = _reading
218✔
890

891
    else
892
      error("TLS/SSL handshake failed: " .. tostring(err))
×
893
    end
894

895
    coroutine_yield(nskt, queue)
218✔
896
  until false
218✔
897
end
898

899
-- flushes a client write buffer (deprecated)
900
function copas.flush()
218✔
901
end
902

903
-- wraps a TCP socket to use Copas methods (send, receive, flush and settimeout)
904
local _skt_mt_tcp = {
218✔
905
      __tostring = function(self)
906
        return tostring(self.socket).." (copas wrapped)"
21✔
907
      end,
908

909
      __index = {
218✔
910
        send = function (self, data, from, to)
911
          return copas.send (self.socket, data, from, to)
17,752✔
912
        end,
913

914
        receive = function (self, pattern, prefix)
915
          if user_timeouts_receive[self.socket] == 0 then
2,669,979✔
916
            return copas.receivepartial(self.socket, pattern, prefix)
14✔
917
          end
918
          return copas.receive(self.socket, pattern, prefix)
2,669,963✔
919
        end,
920

921
        receivepartial = function (self, pattern, prefix)
922
          return copas.receivepartial(self.socket, pattern, prefix)
×
923
        end,
924

925
        flush = function (self)
926
          return copas.flush(self.socket)
×
927
        end,
928

929
        settimeout = function (self, time)
930
          return copas.settimeout(self.socket, time)
231✔
931
        end,
932

933
        settimeouts = function (self, connect, send, receive)
934
          return copas.settimeouts(self.socket, connect, send, receive)
×
935
        end,
936

937
        -- TODO: socket.connect is a shortcut, and must be provided with an alternative
938
        -- if ssl parameters are available, it will also include a handshake
939
        connect = function(self, ...)
940
          local res, err = copas.connect(self.socket, ...)
245✔
941
          if res then
245✔
942
            if self.ssl_params.sni then self:sni() end
224✔
943
            if self.ssl_params.wrap then res, err = self:dohandshake() end
250✔
944
          end
945
          return res, err
238✔
946
        end,
947

948
        close = function(self, ...)
949
          return copas.close(self.socket, ...)
259✔
950
        end,
951

952
        -- TODO: socket.bind is a shortcut, and must be provided with an alternative
953
        bind = function(self, ...) return self.socket:bind(...) end,
218✔
954

955
        -- TODO: is this DNS related? hence blocking?
956
        getsockname = function(self, ...)
957
          local ok, ip, port, family = pcall(self.socket.getsockname, self.socket, ...)
×
958
          if ok then
×
959
            return ip, port, family
×
960
          else
961
            return nil, "not implemented by LuaSec"
×
962
          end
963
        end,
964

965
        getstats = function(self, ...) return self.socket:getstats(...) end,
218✔
966

967
        setstats = function(self, ...) return self.socket:setstats(...) end,
218✔
968

969
        listen = function(self, ...) return self.socket:listen(...) end,
218✔
970

971
        accept = function(self, ...) return self.socket:accept(...) end,
218✔
972

973
        setoption = function(self, ...)
974
          local ok, res, err = pcall(self.socket.setoption, self.socket, ...)
×
975
          if ok then
×
976
            return res, err
×
977
          else
978
            return nil, "not implemented by LuaSec"
×
979
          end
980
        end,
981

982
        getoption = function(self, ...)
983
          local ok, val, err = pcall(self.socket.getoption, self.socket, ...)
×
984
          if ok then
×
985
            return val, err
×
986
          else
987
            return nil, "not implemented by LuaSec"
×
988
          end
989
        end,
990

991
        -- TODO: is this DNS related? hence blocking?
992
        getpeername = function(self, ...)
993
          local ok, ip, port, family = pcall(self.socket.getpeername, self.socket, ...)
×
994
          if ok then
×
995
            return ip, port, family
×
996
          else
997
            return nil, "not implemented by LuaSec"
×
998
          end
999
        end,
1000

1001
        shutdown = function(self, ...) return self.socket:shutdown(...) end,
218✔
1002

1003
        sni = function(self, names, strict)
1004
          local sslp = self.ssl_params
98✔
1005
          self.socket = ssl_wrap(self.socket, sslp.wrap)
126✔
1006
          if names == nil then
98✔
1007
            names = sslp.sni.names
84✔
1008
            strict = sslp.sni.strict
84✔
1009
          end
1010
          return self.socket:sni(names, strict)
98✔
1011
        end,
1012

1013
        dohandshake = function(self, wrap_params)
1014
          local nskt, err = copas.dohandshake(self.socket, wrap_params or self.ssl_params.wrap)
126✔
1015
          if not nskt then return nskt, err end
112✔
1016
          self.socket = nskt  -- replace internal socket with the newly wrapped ssl one
112✔
1017
          return self
112✔
1018
        end,
1019

1020
        getalpn = function(self, ...)
1021
          local ok, proto, err = pcall(self.socket.getalpn, self.socket, ...)
×
1022
          if ok then
×
1023
            return proto, err
×
1024
          else
1025
            return nil, "not a tls socket"
×
1026
          end
1027
        end,
1028

1029
        getsniname = function(self, ...)
1030
          local ok, name, err = pcall(self.socket.getsniname, self.socket, ...)
×
1031
          if ok then
×
1032
            return name, err
×
1033
          else
1034
            return nil, "not a tls socket"
×
1035
          end
1036
        end,
1037
      }
218✔
1038
}
1039

1040
-- wraps a UDP socket, copy of TCP one adapted for UDP.
1041
local _skt_mt_udp = {__index = { }}
218✔
1042
for k,v in pairs(_skt_mt_tcp) do _skt_mt_udp[k] = _skt_mt_udp[k] or v end
654✔
1043
for k,v in pairs(_skt_mt_tcp.__index) do _skt_mt_udp.__index[k] = v end
5,014✔
1044

1045
_skt_mt_udp.__index.send        = function(self, ...) return self.socket:send(...) end
225✔
1046

1047
_skt_mt_udp.__index.sendto      = function(self, ...) return self.socket:sendto(...) end
239✔
1048

1049

1050
_skt_mt_udp.__index.receive =     function (self, size)
218✔
1051
                                    return copas.receive (self.socket, (size or UDP_DATAGRAM_MAX))
14✔
1052
                                  end
1053

1054
_skt_mt_udp.__index.receivefrom = function (self, size)
218✔
1055
                                    return copas.receivefrom (self.socket, (size or UDP_DATAGRAM_MAX))
28✔
1056
                                  end
1057

1058
                                  -- TODO: is this DNS related? hence blocking?
1059
_skt_mt_udp.__index.setpeername = function(self, ...) return self.socket:setpeername(...) end
225✔
1060

1061
_skt_mt_udp.__index.setsockname = function(self, ...) return self.socket:setsockname(...) end
218✔
1062

1063
                                    -- do not close client, as it is also the server for udp.
1064
_skt_mt_udp.__index.close       = function(self, ...) return true end
232✔
1065

1066
_skt_mt_udp.__index.settimeouts = function (self, connect, send, receive)
218✔
1067
                                    return copas.settimeouts(self.socket, connect, send, receive)
×
1068
                                  end
1069

1070

1071

1072
---
1073
-- Wraps a LuaSocket socket object in an async Copas based socket object.
1074
-- @param skt The socket to wrap
1075
-- @sslt (optional) Table with ssl parameters, use an empty table to use ssl with defaults
1076
-- @return wrapped socket object
1077
function copas.wrap (skt, sslt)
218✔
1078
  if (getmetatable(skt) == _skt_mt_tcp) or (getmetatable(skt) == _skt_mt_udp) then
420✔
1079
    return skt -- already wrapped
×
1080
  end
1081

1082
  skt:settimeout(0)
424✔
1083

1084
  if isTCP(skt) then
540✔
1085
    return setmetatable ({socket = skt, ssl_params = normalize_sslt(sslt)}, _skt_mt_tcp)
504✔
1086
  else
1087
    return setmetatable ({socket = skt}, _skt_mt_udp)
28✔
1088
  end
1089
end
1090

1091
--- Wraps a handler in a function that deals with wrapping the socket and doing the
1092
-- optional ssl handshake.
1093
function copas.handler(handler, sslparams)
218✔
1094
  -- TODO: pass a timeout value to set, and use during handshake
1095
  return function (skt, ...)
1096
    skt = copas.wrap(skt, sslparams) -- this call will normalize the sslparams table
144✔
1097
    local sslp = skt.ssl_params
112✔
1098
    if sslp.sni then skt:sni(sslp.sni.names, sslp.sni.strict) end
112✔
1099
    if sslp.wrap then skt:dohandshake(sslp.wrap) end
112✔
1100
    return handler(skt, ...)
105✔
1101
  end
1102
end
1103

1104

1105
--------------------------------------------------
1106
-- Error handling
1107
--------------------------------------------------
1108

1109
local _errhandlers = setmetatable({}, { __mode = "k" })   -- error handler per coroutine
218✔
1110

1111

1112
function copas.gettraceback(msg, co, skt)
218✔
1113
  local co_str = co == nil and "nil" or copas.getthreadname(co)
43✔
1114
  local skt_str = skt == nil and "nil" or copas.getsocketname(skt)
43✔
1115
  local msg_str = msg == nil and "" or tostring(msg)
43✔
1116
  if msg_str == "" then
43✔
1117
    msg_str = ("(coroutine: %s, socket: %s)"):format(msg_str, co_str, skt_str)
×
1118
  else
1119
    msg_str = ("%s (coroutine: %s, socket: %s)"):format(msg_str, co_str, skt_str)
43✔
1120
  end
1121

1122
  if type(co) == "thread" then
43✔
1123
    -- regular Copas coroutine
1124
    return debug.traceback(co, msg_str)
43✔
1125
  end
1126
  -- not a coroutine, but the main thread, this happens if a timeout callback
1127
  -- (see `copas.timeout` causes an error (those callbacks run on the main thread).
1128
  return debug.traceback(msg_str, 2)
×
1129
end
1130

1131

1132
local function _deferror(msg, co, skt)
1133
  print(copas.gettraceback(msg, co, skt))
35✔
1134
end
1135

1136

1137
function copas.seterrorhandler(err, default)
218✔
1138
  assert(err == nil or type(err) == "function", "Expected the handler to be a function, or nil")
70✔
1139
  if default then
70✔
1140
    assert(err ~= nil, "Expected the handler to be a function when setting the default")
49✔
1141
    _deferror = err
49✔
1142
  else
1143
    _errhandlers[coroutine_running()] = err
21✔
1144
  end
1145
end
1146
copas.setErrorHandler = copas.seterrorhandler  -- deprecated; old casing
218✔
1147

1148

1149
function copas.geterrorhandler(co)
218✔
1150
  co = co or coroutine_running()
14✔
1151
  return _errhandlers[co] or _deferror
14✔
1152
end
1153

1154

1155
-- if `bool` is truthy, then the original socket errors will be returned in case of timeouts;
1156
-- `timeout, wantread, wantwrite, Operation already in progress`. If falsy, it will always
1157
-- return `timeout`.
1158
function copas.useSocketTimeoutErrors(bool)
218✔
1159
  useSocketTimeoutErrors[coroutine_running()] = not not bool -- force to a boolean
7✔
1160
end
1161

1162
-------------------------------------------------------------------------------
1163
-- Thread handling
1164
-------------------------------------------------------------------------------
1165

1166
local function _doTick (co, skt, ...)
1167
  if not co then return end
305,918✔
1168

1169
  -- if a coroutine was canceled/removed, don't resume it
1170
  if _canceled[co] then
305,918✔
1171
    _canceled[co] = nil -- also clean up the registry
29✔
1172
    _threads[co] = nil
29✔
1173
    return
29✔
1174
  end
1175

1176
  -- res: the socket (being read/write on) or the time to sleep
1177
  -- new_q: either _writing, _reading, or _sleeping
1178
  -- local time_before = gettime()
1179
  local ok, res, new_q = coroutine_resume(co, skt, ...)
305,889✔
1180
  -- local duration = gettime() - time_before
1181
  -- if duration > 1 then
1182
  --   duration = math.floor(duration * 1000)
1183
  --   pcall(_errhandlers[co] or _deferror, "task ran for "..tostring(duration).." milliseconds.", co, skt)
1184
  -- end
1185

1186
  if new_q == _reading or new_q == _writing or new_q == _sleeping then
305,882✔
1187
    -- we're yielding to a new queue
1188
    new_q:insert (res)
299,798✔
1189
    new_q:push (res, co)
299,798✔
1190
    return
299,798✔
1191
  end
1192

1193
  -- coroutine is terminating
1194

1195
  if ok and coroutine_status(co) ~= "dead" then
6,084✔
1196
    -- it called coroutine.yield from a non-Copas function which is unexpected
1197
    ok = false
7✔
1198
    res = "coroutine.yield was called without a resume first, user-code cannot yield to Copas"
7✔
1199
  end
1200

1201
  if not ok then
6,084✔
1202
    local k, e = pcall(_errhandlers[co] or _deferror, res, co, skt)
53✔
1203
    if not k then
53✔
1204
      print("Failed executing error handler: " .. tostring(e))
×
1205
    end
1206
  end
1207

1208
  local skt_to_close = _autoclose[co]
6,084✔
1209
  if skt_to_close then
6,084✔
1210
    skt_to_close:close()
140✔
1211
    _autoclose[co] = nil
140✔
1212
    _autoclose_r[skt_to_close] = nil
140✔
1213
  end
1214

1215
  _errhandlers[co] = nil
6,084✔
1216
end
1217

1218

1219
local _accept do
218✔
1220
  local client_counters = setmetatable({}, { __mode = "k" })
218✔
1221

1222
  -- accepts a connection on socket input
1223
  function _accept(server_skt, handler)
188✔
1224
    local client_skt = server_skt:accept()
147✔
1225
    if client_skt then
147✔
1226
      local count = (client_counters[server_skt] or 0) + 1
147✔
1227
      client_counters[server_skt] = count
147✔
1228
      object_names[client_skt] = object_names[server_skt] .. ":client_" .. count
175✔
1229

1230
      client_skt:settimeout(0)
147✔
1231
      copas.settimeouts(client_skt, user_timeouts_connect[server_skt],  -- copy server socket timeout settings
294✔
1232
        user_timeouts_send[server_skt], user_timeouts_receive[server_skt])
175✔
1233

1234
      local co = coroutine_create(handler)
147✔
1235
      object_names[co] = object_names[server_skt] .. ":handler_" .. count
147✔
1236

1237
      if copas.autoclose then
147✔
1238
        _autoclose[co] = client_skt
147✔
1239
        _autoclose_r[client_skt] = co
147✔
1240
      end
1241

1242
      _doTick(co, client_skt)
147✔
1243
    end
1244
  end
1245
end
1246

1247
-------------------------------------------------------------------------------
1248
-- Adds a server/handler pair to Copas dispatcher
1249
-------------------------------------------------------------------------------
1250

1251
do
1252
  local function addTCPserver(server, handler, timeout, name)
1253
    server:settimeout(0)
112✔
1254
    if name then
112✔
1255
      object_names[server] = name
×
1256
    end
1257
    _servers[server] = handler
112✔
1258
    _reading:insert(server)
112✔
1259
    if timeout then
112✔
1260
      copas.settimeout(server, timeout)
21✔
1261
    end
1262
  end
1263

1264
  local function addUDPserver(server, handler, timeout, name)
1265
    server:settimeout(0)
×
1266
    local co = coroutine_create(handler)
×
1267
    if name then
×
1268
      object_names[server] = name
×
1269
    end
1270
    object_names[co] = object_names[server]..":handler"
×
1271
    _reading:insert(server)
×
1272
    if timeout then
×
1273
      copas.settimeout(server, timeout)
×
1274
    end
1275
    _doTick(co, server)
×
1276
  end
1277

1278

1279
  function copas.addserver(server, handler, timeout, name)
218✔
1280
    if isTCP(server) then
144✔
1281
      addTCPserver(server, handler, timeout, name)
144✔
1282
    else
1283
      addUDPserver(server, handler, timeout, name)
×
1284
    end
1285
  end
1286
end
1287

1288

1289
function copas.removeserver(server, keep_open)
218✔
1290
  local skt = server
105✔
1291
  local mt = getmetatable(server)
105✔
1292
  if mt == _skt_mt_tcp or mt == _skt_mt_udp then
105✔
1293
    skt = server.socket
×
1294
  end
1295

1296
  _servers:remove(skt)
105✔
1297
  _reading:remove(skt)
105✔
1298

1299
  if keep_open then
105✔
1300
    return true
21✔
1301
  end
1302
  return server:close()
84✔
1303
end
1304

1305

1306

1307
-------------------------------------------------------------------------------
1308
-- Adds an new coroutine thread to Copas dispatcher
1309
-------------------------------------------------------------------------------
1310
function copas.addnamedthread(name, handler, ...)
218✔
1311
  if type(name) == "function" and type(handler) == "string" then
6,232✔
1312
    -- old call, flip args for compatibility
1313
    name, handler = handler, name
×
1314
  end
1315

1316
  -- create a coroutine that skips the first argument, which is always the socket
1317
  -- passed by the scheduler, but `nil` in case of a task/thread
1318
  local thread = coroutine_create(function(_, ...)
12,464✔
1319
    copas.pause()
6,232✔
1320
    return handler(...)
6,211✔
1321
  end)
1322
  if name then
6,232✔
1323
    object_names[thread] = name
534✔
1324
  end
1325

1326
  _threads[thread] = true -- register this thread so it can be removed
6,232✔
1327
  _doTick (thread, nil, ...)
6,232✔
1328
  return thread
6,232✔
1329
end
1330

1331

1332
function copas.addthread(handler, ...)
218✔
1333
  return copas.addnamedthread(nil, handler, ...)
5,621✔
1334
end
1335

1336

1337
function copas.removethread(thread)
218✔
1338
  -- if the specified coroutine is registered, add it to the canceled table so
1339
  -- that next time it tries to resume it exits.
1340
  _canceled[thread] = _threads[thread or 0]
63✔
1341
  _sleeping:cancel(thread)
63✔
1342
end
1343

1344

1345

1346
-------------------------------------------------------------------------------
1347
-- Sleep/pause management functions
1348
-------------------------------------------------------------------------------
1349

1350
-- yields the current coroutine and wakes it after 'sleeptime' seconds.
1351
-- If sleeptime < 0 then it sleeps until explicitly woken up using 'wakeup'
1352
-- TODO: deprecated, remove in next major
1353
function copas.sleep(sleeptime)
218✔
1354
  coroutine_yield((sleeptime or 0), _sleeping)
×
1355
end
1356

1357

1358
-- yields the current coroutine and wakes it after 'sleeptime' seconds.
1359
-- if sleeptime < 0 then it sleeps 0 seconds.
1360
function copas.pause(sleeptime)
218✔
1361
  local s = gettime()
294,579✔
1362
  if sleeptime and sleeptime > 0 then
294,579✔
1363
    coroutine_yield(sleeptime, _sleeping)
10,988✔
1364
  else
1365
    coroutine_yield(0, _sleeping)
285,998✔
1366
  end
1367
  return gettime() - s
294,312✔
1368
end
1369

1370

1371
-- yields the current coroutine until explicitly woken up using 'wakeup'
1372
function copas.pauseforever()
218✔
1373
  local s = gettime()
3,759✔
1374
  coroutine_yield(-1, _sleeping)
3,759✔
1375
  return gettime() - s
3,738✔
1376
end
1377

1378

1379
-- Wakes up a sleeping coroutine 'co'.
1380
function copas.wakeup(co)
218✔
1381
  _sleeping:wakeup(co)
3,773✔
1382
end
1383

1384

1385

1386
-------------------------------------------------------------------------------
1387
-- Timeout management
1388
-------------------------------------------------------------------------------
1389

1390
do
1391
  local timeout_register = setmetatable({}, { __mode = "k" })
218✔
1392
  local timerwheel = require("timerwheel").new({
438✔
1393
      now = gettime,
218✔
1394
      precision = TIMEOUT_PRECISION,
218✔
1395
      ringsize = math.floor(60*60*24/TIMEOUT_PRECISION),  -- ring size 1 day
218✔
1396
      err_handler = function(err)
1397
        return _deferror(err, core_timer_thread)
18✔
1398
      end,
1399
    })
1400

1401
  core_timer_thread = copas.addnamedthread("copas_core_timer", function()
436✔
1402
    while true do
1403
      copas.pause(TIMEOUT_PRECISION)
7,508✔
1404
      timerwheel:step()
9,405✔
1405
    end
1406
  end)
1407

1408
  -- get the number of timeouts running
1409
  function copas.gettimeouts()
218✔
1410
    return timerwheel:count()
3,150✔
1411
  end
1412

1413
  --- Sets the timeout for the current coroutine.
1414
  -- @param delay delay (seconds), use 0 (or math.huge) to cancel the timerout
1415
  -- @param callback function with signature: `function(coroutine)` where coroutine is the routine that timed-out
1416
  -- @return true
1417
  function copas.timeout(delay, callback)
218✔
1418
    local co = coroutine_running()
5,382,028✔
1419
    local existing_timer = timeout_register[co]
5,382,028✔
1420

1421
    if existing_timer then
5,382,028✔
1422
      timerwheel:cancel(existing_timer)
5,088✔
1423
    end
1424

1425
    if delay > 0 and delay ~= math.huge then
5,382,028✔
1426
      timeout_register[co] = timerwheel:set(delay, callback, co)
8,863✔
1427
    elseif delay == 0 or delay == math.huge then
5,375,144✔
1428
      timeout_register[co] = nil
5,375,144✔
1429
    else
1430
      error("timout value must be greater than or equal to 0, got: "..tostring(delay))
×
1431
    end
1432

1433
    return true
5,382,028✔
1434
  end
1435

1436
end
1437

1438

1439
-------------------------------------------------------------------------------
1440
-- main tasks: manage readable and writable socket sets
1441
-------------------------------------------------------------------------------
1442
-- a task is an object with a required method `step()` that deals with a
1443
-- single step for that task.
1444

1445
local _tasks = {} do
218✔
1446
  function _tasks:add(tsk)
218✔
1447
    _tasks[#_tasks + 1] = tsk
872✔
1448
  end
1449
end
1450

1451

1452
-- a task to check ready to read events
1453
local _readable_task = {} do
218✔
1454

1455
  _readable_task._events = {}
218✔
1456

1457
  local function tick(skt)
1458
    local handler = _servers[skt]
1,080✔
1459
    if handler then
1,080✔
1460
      _accept(skt, handler)
189✔
1461
    else
1462
      _reading:remove(skt)
933✔
1463
      _doTick(_reading:pop(skt), skt)
1,232✔
1464
    end
1465
  end
1466

1467
  function _readable_task:step()
218✔
1468
    for _, skt in ipairs(self._events) do
288,455✔
1469
      tick(skt)
1,080✔
1470
    end
1471
  end
1472

1473
  _tasks:add(_readable_task)
278✔
1474
end
1475

1476

1477
-- a task to check ready to write events
1478
local _writable_task = {} do
218✔
1479

1480
  _writable_task._events = {}
218✔
1481

1482
  local function tick(skt)
1483
    _writing:remove(skt)
436✔
1484
    _doTick(_writing:pop(skt), skt)
562✔
1485
  end
1486

1487
  function _writable_task:step()
218✔
1488
    for _, skt in ipairs(self._events) do
287,806✔
1489
      tick(skt)
436✔
1490
    end
1491
  end
1492

1493
  _tasks:add(_writable_task)
278✔
1494
end
1495

1496

1497

1498
-- sleeping threads task
1499
local _sleeping_task = {} do
218✔
1500

1501
  function _sleeping_task:step()
218✔
1502
    local now = gettime()
287,370✔
1503

1504
    local co = _sleeping:pop(now)
287,370✔
1505
    while co do
295,708✔
1506
      -- we're pushing them to _resumable, since that list will be replaced before
1507
      -- executing. This prevents tasks running twice in a row with pause(0) for example.
1508
      -- So here we won't execute, but at _resumable step which is next
1509
      _resumable:push(co)
8,338✔
1510
      co = _sleeping:pop(now)
10,747✔
1511
    end
1512
  end
1513

1514
  _tasks:add(_sleeping_task)
218✔
1515
end
1516

1517

1518

1519
-- resumable threads task
1520
local _resumable_task = {} do
218✔
1521

1522
  function _resumable_task:step()
218✔
1523
    -- replace the resume list before iterating, so items placed in there
1524
    -- will indeed end up in the next copas step, not in this one, and not
1525
    -- create a loop
1526
    local resumelist = _resumable:clear_resumelist()
287,370✔
1527

1528
    for _, co in ipairs(resumelist) do
585,538✔
1529
      _doTick(co)
298,170✔
1530
    end
1531
  end
1532

1533
  _tasks:add(_resumable_task)
218✔
1534
end
1535

1536

1537
-------------------------------------------------------------------------------
1538
-- Checks for reads and writes on sockets
1539
-------------------------------------------------------------------------------
1540
local _select_plain do
218✔
1541

1542
  local last_cleansing = 0
218✔
1543
  local duration = function(t2, t1) return t2-t1 end
287,498✔
1544

1545
  if not socket then
218✔
1546
    -- socket module unavailable, switch to luasystem sleep
1547
    _select_plain = block_sleep
7✔
1548
  else
1549
    -- use socket.select to handle socket-io
1550
    _select_plain = function(timeout)
1551
      local err
1552
      local now = gettime()
287,280✔
1553

1554
      -- remove any closed sockets to prevent select from hanging on them
1555
      if _closed[1] then
287,280✔
1556
        for i, skt in ipairs(_closed) do
515✔
1557
          _closed[i] = { _reading:remove(skt), _writing:remove(skt) }
407✔
1558
        end
1559
      end
1560

1561
      _readable_task._events, _writable_task._events, err = socket.select(_reading, _writing, timeout)
287,280✔
1562
      local r_events, w_events = _readable_task._events, _writable_task._events
287,280✔
1563

1564
      -- inject closed sockets in readable/writeable task so they can error out properly
1565
      if _closed[1] then
287,280✔
1566
        for i, skts in ipairs(_closed) do
515✔
1567
          _closed[i] = nil
259✔
1568
          r_events[#r_events+1] = skts[1]
259✔
1569
          w_events[#w_events+1] = skts[2]
259✔
1570
        end
1571
      end
1572

1573
      if duration(now, last_cleansing) > WATCH_DOG_TIMEOUT then
420,199✔
1574
        last_cleansing = now
204✔
1575

1576
        -- Check all sockets selected for reading, and check how long they have been waiting
1577
        -- for data already, without select returning them as readable
1578
        for skt,time in pairs(_reading_log) do
204✔
1579
          if not r_events[skt] and duration(now, time) > WATCH_DOG_TIMEOUT then
×
1580
            -- This one timedout while waiting to become readable, so move
1581
            -- it in the readable list and try and read anyway, despite not
1582
            -- having been returned by select
1583
            _reading_log[skt] = nil
×
1584
            r_events[#r_events + 1] = skt
×
1585
            r_events[skt] = #r_events
×
1586
          end
1587
        end
1588

1589
        -- Do the same for writing
1590
        for skt,time in pairs(_writing_log) do
204✔
1591
          if not w_events[skt] and duration(now, time) > WATCH_DOG_TIMEOUT then
×
1592
            _writing_log[skt] = nil
×
1593
            w_events[#w_events + 1] = skt
×
1594
            w_events[skt] = #w_events
×
1595
          end
1596
        end
1597
      end
1598

1599
      if err == "timeout" and #r_events + #w_events > 0 then
287,280✔
1600
        return nil
7✔
1601
      else
1602
        return err
287,273✔
1603
      end
1604
    end
1605
  end
1606
end
1607

1608

1609

1610
-------------------------------------------------------------------------------
1611
-- Dispatcher loop step.
1612
-- Listen to client requests and handles them
1613
-- Returns false if no socket-data was handled, or true if there was data
1614
-- handled (or nil + error message)
1615
-------------------------------------------------------------------------------
1616

1617
local copas_stats
1618
local min_ever, max_ever
1619

1620
local _select = _select_plain
218✔
1621

1622
-- instrumented version of _select() to collect stats
1623
local _select_instrumented = function(timeout)
1624
  if copas_stats then
×
1625
    local step_duration = gettime() - copas_stats.step_start
×
1626
    copas_stats.duration_max = math.max(copas_stats.duration_max, step_duration)
×
1627
    copas_stats.duration_min = math.min(copas_stats.duration_min, step_duration)
×
1628
    copas_stats.duration_tot = copas_stats.duration_tot + step_duration
×
1629
    copas_stats.steps = copas_stats.steps + 1
×
1630
  else
1631
    copas_stats = {
×
1632
      duration_max = -1,
1633
      duration_min = 999999,
1634
      duration_tot = 0,
1635
      steps = 0,
1636
    }
1637
  end
1638

1639
  local err = _select_plain(timeout)
×
1640

1641
  local now = gettime()
×
1642
  copas_stats.time_start = copas_stats.time_start or now
×
1643
  copas_stats.step_start = now
×
1644

1645
  return err
×
1646
end
1647

1648

1649
function copas.step(timeout)
218✔
1650
  -- Need to wake up the select call in time for the next sleeping event
1651
  if not _resumable:done() then
420,318✔
1652
    timeout = 0
280,335✔
1653
  else
1654
    timeout = math.min(_sleeping:getnext(), timeout or math.huge)
9,065✔
1655
  end
1656

1657
  local err = _select(timeout)
287,375✔
1658

1659
  for _, tsk in ipairs(_tasks) do
1,436,858✔
1660
    tsk:step()
1,149,490✔
1661
  end
1662

1663
  if err then
287,368✔
1664
    if err == "timeout" then
286,041✔
1665
      if timeout + 0.01 > TIMEOUT_PRECISION and math.random(100) > 90 then
285,946✔
1666
        -- we were idle, so occasionally do a GC sweep to ensure lingering
1667
        -- sockets are closed, and we don't accidentally block the loop from
1668
        -- exiting
1669
        collectgarbage()
461✔
1670
      end
1671
      return false
285,946✔
1672
    end
1673
    return nil, err
95✔
1674
  end
1675

1676
  return true
1,327✔
1677
end
1678

1679

1680
-------------------------------------------------------------------------------
1681
-- Check whether there is something to do.
1682
-- returns false if there are no sockets for read/write nor tasks scheduled
1683
-- (which means Copas is in an empty spin)
1684
-------------------------------------------------------------------------------
1685
function copas.finished()
218✔
1686
  return #_reading == 0 and #_writing == 0 and _resumable:done() and _sleeping:done(copas.gettimeouts())
290,250✔
1687
end
1688

1689

1690
local resetexit do
218✔
1691
  local exit_semaphore, exiting
1692

1693
  function resetexit()
188✔
1694
    exit_semaphore = copas.semaphore.new(1, 0, math.huge)
512✔
1695
    exiting = false
358✔
1696
  end
1697

1698
  -- Signals tasks to exit. But only if they check for it. By calling `copas.exiting`
1699
  -- they can check if they should exit. Or by calling `copas.waitforexit` they can
1700
  -- wait until the exit signal is given.
1701
  function copas.exit()
218✔
1702
    if exiting then return end
351✔
1703
    exiting = true
351✔
1704
    exit_semaphore:destroy()
351✔
1705
  end
1706

1707
  -- returns whether Copas is in the process of exiting. Exit can be started by
1708
  -- calling `copas.exit()`.
1709
  function copas.exiting()
218✔
1710
    return exiting
695✔
1711
  end
1712

1713
  -- Pauses the current coroutine until Copas is exiting. To be used as an exit
1714
  -- signal for tasks that need to clean up before exiting.
1715
  function copas.waitforexit()
218✔
1716
    exit_semaphore:take(1)
14✔
1717
  end
1718
end
1719

1720

1721
--- Forcibly cancels all pending work and signals exit.
1722
-- Intended for test teardown only. Abandons all registered threads and sockets
1723
-- without giving them a chance to clean up. After this call copas.finished()
1724
-- will return true and the loop will exit. The module is left in a clean state
1725
-- ready for the next copas.loop() call.
1726
function copas.cancelall()
218✔
1727
  -- 1. clear resumable queue
1728
  _resumable:clear_resumelist()
×
1729

1730
  -- 2. drain sleeping heap
1731
  _sleeping:cancelall()
×
1732

1733
  -- 3. close and drain reading sockets
1734
  while _reading[1] do
×
1735
    copas.close(_reading[1])
×
1736
    _reading:remove(_reading[1])
×
1737
  end
1738

1739
  -- 4. close and drain writing sockets
1740
  while _writing[1] do
×
1741
    copas.close(_writing[1])
×
1742
    _writing:remove(_writing[1])
×
1743
  end
1744

1745
  -- 5. remove all servers
1746
  while _servers[1] do
×
1747
    copas.removeserver(_servers[1])
×
1748
  end
1749

1750
  -- 6. clear non-weak ancillary tables
1751
  _closed = {}
×
1752
  _reading_log = {}
×
1753
  _writing_log = {}
×
1754

1755
  -- 7. signal exit
1756
  copas.exit()
×
1757
end
1758

1759

1760
local _getstats do
218✔
1761
  local _getstats_instrumented, _getstats_plain
1762

1763

1764
  function _getstats_plain(enable)
188✔
1765
    -- this function gets hit if turned off, so turn on if true
1766
    if enable == true then
×
1767
      _select = _select_instrumented
×
1768
      _getstats = _getstats_instrumented
×
1769
      -- reset stats
1770
      min_ever = nil
×
1771
      max_ever = nil
×
1772
      copas_stats = nil
×
1773
    end
1774
    return {}
×
1775
  end
1776

1777

1778
  -- convert from seconds to millisecs, with microsec precision
1779
  local function useconds(t)
1780
    return math.floor((t * 1000000) + 0.5) / 1000
×
1781
  end
1782
  -- convert from seconds to seconds, with millisec precision
1783
  local function mseconds(t)
1784
    return math.floor((t * 1000) + 0.5) / 1000
×
1785
  end
1786

1787

1788
  function _getstats_instrumented(enable)
188✔
1789
    if enable == false then
×
1790
      _select = _select_plain
×
1791
      _getstats = _getstats_plain
×
1792
      -- instrumentation disabled, so switch to the plain implementation
1793
      return _getstats(enable)
×
1794
    end
1795
    if (not copas_stats) or (copas_stats.step == 0) then
×
1796
      return {}
×
1797
    end
1798
    local stats = copas_stats
×
1799
    copas_stats = nil
×
1800
    min_ever = math.min(min_ever or 9999999, stats.duration_min)
×
1801
    max_ever = math.max(max_ever or 0, stats.duration_max)
×
1802
    stats.duration_min_ever = min_ever
×
1803
    stats.duration_max_ever = max_ever
×
1804
    stats.duration_avg = stats.duration_tot / stats.steps
×
1805
    stats.step_start = nil
×
1806
    stats.time_end = gettime()
×
1807
    stats.time_tot = stats.time_end - stats.time_start
×
1808
    stats.time_avg = stats.time_tot / stats.steps
×
1809

1810
    stats.duration_avg = useconds(stats.duration_avg)
×
1811
    stats.duration_max = useconds(stats.duration_max)
×
1812
    stats.duration_max_ever = useconds(stats.duration_max_ever)
×
1813
    stats.duration_min = useconds(stats.duration_min)
×
1814
    stats.duration_min_ever = useconds(stats.duration_min_ever)
×
1815
    stats.duration_tot = useconds(stats.duration_tot)
×
1816
    stats.time_avg = useconds(stats.time_avg)
×
1817
    stats.time_start = mseconds(stats.time_start)
×
1818
    stats.time_end = mseconds(stats.time_end)
×
1819
    stats.time_tot = mseconds(stats.time_tot)
×
1820
    return stats
×
1821
  end
1822

1823
  _getstats = _getstats_plain
218✔
1824
end
1825

1826

1827
function copas.status(enable_stats)
218✔
1828
  local res = _getstats(enable_stats)
×
1829
  res.running = not not copas.running
×
1830
  res.timeout = copas.gettimeouts()
×
1831
  res.timer, res.inactive = _sleeping:status()
×
1832
  res.read = #_reading
×
1833
  res.write = #_writing
×
1834
  res.active = _resumable:count()
×
1835
  return res
×
1836
end
1837

1838

1839
-------------------------------------------------------------------------------
1840
-- Dispatcher endless loop.
1841
-- Listen to client requests and handles them forever
1842
-------------------------------------------------------------------------------
1843
function copas.loop(initializer, timeout)
278✔
1844
  if type(initializer) == "function" then
358✔
1845
    copas.addnamedthread("copas_initializer", initializer)
179✔
1846
  else
1847
    timeout = initializer or timeout
217✔
1848
  end
1849

1850
  resetexit()
358✔
1851
  copas.running = true
358✔
1852
  while true do
1853
    copas.step(timeout)
287,375✔
1854
    if copas.finished() then
420,309✔
1855
      if copas.exiting() then
889✔
1856
        break
198✔
1857
      end
1858
      copas.exit()
344✔
1859
    end
1860
  end
1861
  copas.running = false
351✔
1862
end
1863

1864

1865
-------------------------------------------------------------------------------
1866
-- Naming sockets and coroutines.
1867
-------------------------------------------------------------------------------
1868
do
1869
  local function realsocket(skt)
1870
    local mt = getmetatable(skt)
105✔
1871
    if mt == _skt_mt_tcp or mt == _skt_mt_udp then
105✔
1872
      return skt.socket
105✔
1873
    else
1874
      return skt
×
1875
    end
1876
  end
1877

1878

1879
  function copas.setsocketname(name, skt)
278✔
1880
    assert(type(name) == "string", "expected arg #1 to be a string")
105✔
1881
    skt = assert(realsocket(skt), "expected arg #2 to be a socket")
135✔
1882
    object_names[skt] = name
105✔
1883
  end
1884

1885

1886
  function copas.getsocketname(skt)
278✔
1887
    skt = assert(realsocket(skt), "expected arg #1 to be a socket")
×
1888
    return object_names[skt]
×
1889
  end
1890
end
1891

1892

1893
function copas.setthreadname(name, coro)
278✔
1894
  assert(type(name) == "string", "expected arg #1 to be a string")
70✔
1895
  coro = coro or coroutine_running()
70✔
1896
  assert(type(coro) == "thread", "expected arg #2 to be a coroutine or nil")
70✔
1897
  object_names[coro] = name
70✔
1898
end
1899

1900

1901
function copas.getthreadname(coro)
278✔
1902
  coro = coro or coroutine_running()
43✔
1903
  assert(type(coro) == "thread", "expected arg #1 to be a coroutine or nil")
43✔
1904
  return object_names[coro]
47✔
1905
end
1906

1907
-------------------------------------------------------------------------------
1908
-- Debug functionality.
1909
-------------------------------------------------------------------------------
1910
do
1911
  copas.debug = {}
218✔
1912

1913
  local log_core    -- if truthy, the core-timer will also be logged
1914
  local debug_log   -- function used as logger
1915

1916

1917
  local debug_yield = function(skt, queue)
1918
    local name = object_names[coroutine_running()]
6,982✔
1919

1920
    if log_core or name ~= "copas_core_timer" then
6,982✔
1921
      if queue == _sleeping then
6,955✔
1922
        debug_log("yielding '", name, "' to SLEEP for ", skt," seconds")
6,857✔
1923

1924
      elseif queue == _writing then
98✔
1925
        debug_log("yielding '", name, "' to WRITE on '", object_names[skt], "'")
18✔
1926

1927
      elseif queue == _reading then
84✔
1928
        debug_log("yielding '", name, "' to READ on '", object_names[skt], "'")
88✔
1929

1930
      else
1931
        debug_log("thread '", name, "' yielding to unexpected queue; ", tostring(queue), " (", type(queue), ")", debug.traceback())
×
1932
      end
1933
    end
1934

1935
    return coroutine.yield(skt, queue)
6,982✔
1936
  end
1937

1938

1939
  local debug_resume = function(coro, skt, ...)
1940
    local name = object_names[coro]
6,996✔
1941

1942
    if skt then
6,996✔
1943
      debug_log("resuming '", name, "' for socket '", object_names[skt], "'")
98✔
1944
    else
1945
      if log_core or name ~= "copas_core_timer" then
6,898✔
1946
        debug_log("resuming '", name, "'")
6,871✔
1947
      end
1948
    end
1949
    return coroutine.resume(coro, skt, ...)
6,996✔
1950
  end
1951

1952

1953
  local debug_create = function(f)
1954
    local f_wrapped = function(...)
1955
      local results = pack(f(...))
18✔
1956
      debug_log("exiting '", object_names[coroutine_running()], "'")
14✔
1957
      return unpack(results)
14✔
1958
    end
1959

1960
    return coroutine.create(f_wrapped)
14✔
1961
  end
1962

1963

1964
  debug_log = fnil
218✔
1965

1966

1967
  -- enables debug output for all coroutine operations.
1968
  function copas.debug.start(logger, core)
436✔
1969
    log_core = core
7✔
1970
    debug_log = logger or print
7✔
1971
    coroutine_yield = debug_yield
7✔
1972
    coroutine_resume = debug_resume
7✔
1973
    coroutine_create = debug_create
7✔
1974
  end
1975

1976

1977
  -- disables debug output for coroutine operations.
1978
  function copas.debug.stop()
436✔
1979
    debug_log = fnil
×
1980
    coroutine_yield = coroutine.yield
×
1981
    coroutine_resume = coroutine.resume
×
1982
    coroutine_create = coroutine.create
×
1983
  end
1984

1985
  do
1986
    local call_id = 0
218✔
1987

1988
    -- Description table of socket functions for debug output.
1989
    -- each socket function name has TWO entries;
1990
    -- 'name_in' and 'name_out', each being an array of names/descriptions of respectively
1991
    -- input parameters and return values.
1992
    -- If either table has a 'callback' key, then that is a function that will be called
1993
    -- with the parameters/return-values for further inspection.
1994
    local args = {
218✔
1995
      settimeout_in = {
218✔
1996
        "socket ",
158✔
1997
        "seconds",
158✔
1998
        "mode   ",
1999
      },
218✔
2000
      settimeout_out = {
218✔
2001
        "success",
158✔
2002
        "error  ",
2003
      },
218✔
2004
      connect_in = {
218✔
2005
        "socket ",
158✔
2006
        "address",
158✔
2007
        "port   ",
2008
      },
218✔
2009
      connect_out = {
218✔
2010
        "success",
158✔
2011
        "error  ",
2012
      },
218✔
2013
      getfd_in = {
218✔
2014
        "socket ",
2015
        -- callback = function(...)
2016
        --   print(debug.traceback("called from:", 4))
2017
        -- end,
2018
      },
218✔
2019
      getfd_out = {
218✔
2020
        "fd",
2021
      },
218✔
2022
      send_in = {
218✔
2023
        "socket   ",
158✔
2024
        "data     ",
158✔
2025
        "idx-start",
158✔
2026
        "idx-end  ",
2027
      },
218✔
2028
      send_out = {
218✔
2029
        "last-idx-send    ",
158✔
2030
        "error            ",
158✔
2031
        "err-last-idx-send",
2032
      },
218✔
2033
      receive_in = {
218✔
2034
        "socket ",
158✔
2035
        "pattern",
158✔
2036
        "prefix ",
2037
      },
218✔
2038
      receive_out = {
218✔
2039
        "received    ",
158✔
2040
        "error       ",
158✔
2041
        "partial data",
2042
      },
218✔
2043
      dirty_in = {
218✔
2044
        "socket",
2045
        -- callback = function(...)
2046
        --   print(debug.traceback("called from:", 4))
2047
        -- end,
2048
      },
218✔
2049
      dirty_out = {
218✔
2050
        "data in read-buffer",
2051
      },
218✔
2052
      close_in = {
218✔
2053
        "socket",
2054
        -- callback = function(...)
2055
        --   print(debug.traceback("called from:", 4))
2056
        -- end,
2057
      },
218✔
2058
      close_out = {
218✔
2059
        "success",
158✔
2060
        "error",
2061
      },
218✔
2062
    }
2063
    local function print_call(func, msg, ...)
2064
      print(msg)
1,012✔
2065
      local arg = pack(...)
1,012✔
2066
      local desc = args[func] or {}
1,012✔
2067
      for i = 1, math.max(arg.n, #desc) do
2,192✔
2068
        local value = arg[i]
1,180✔
2069
        if type(value) == "string" then
1,180✔
2070
          local xvalue = value:sub(1,30)
42✔
2071
          if xvalue ~= value then
42✔
2072
            xvalue = xvalue .."(...truncated)"
×
2073
          end
2074
          print("\t"..(desc[i] or i)..": '"..tostring(xvalue).."' ("..type(value).." #"..#value..")")
42✔
2075
        else
2076
          print("\t"..(desc[i] or i)..": '"..tostring(value).."' ("..type(value)..")")
1,138✔
2077
        end
2078
      end
2079
      if desc.callback then
1,012✔
2080
        desc.callback(...)
×
2081
      end
2082
    end
2083

2084
    local debug_mt = {
218✔
2085
      __index = function(self, key)
2086
        local value = self.__original_socket[key]
506✔
2087
        if type(value) ~= "function" then
506✔
2088
          return value
×
2089
        end
2090
        return function(self2, ...)
2091
            local my_id = call_id + 1
506✔
2092
            call_id = my_id
506✔
2093
            local results
2094

2095
            if self2 ~= self then
506✔
2096
              -- there is no self
2097
              print_call(tostring(key).."_in", my_id .. "-calling '"..tostring(key) .. "' with; ", self, ...)
×
2098
              results = pack(value(self, ...))
×
2099
            else
2100
              print_call(tostring(key).."_in", my_id .. "-calling '" .. tostring(key) .. "' with; ", self.__original_socket, ...)
506✔
2101
              results = pack(value(self.__original_socket, ...))
794✔
2102
            end
2103
            print_call(tostring(key).."_out", my_id .. "-results '"..tostring(key) .. "' returned; ", unpack(results))
794✔
2104
            return unpack(results)
506✔
2105
          end
2106
      end,
2107
      __tostring = function(self)
2108
        return tostring(self.__original_socket)
56✔
2109
      end
2110
    }
2111

2112

2113
    -- wraps a socket (copas or luasocket) in a debug version printing all calls
2114
    -- and their parameters/return values. Extremely noisy!
2115
    -- returns the wrapped socket.
2116
    -- NOTE: only for plain sockets, will not support TLS
2117
    function copas.debug.socket(original_skt)
436✔
2118
      if (getmetatable(original_skt) == _skt_mt_tcp) or (getmetatable(original_skt) == _skt_mt_udp) then
14✔
2119
        -- already wrapped as Copas socket, so recurse with the original luasocket one
2120
        original_skt.socket = copas.debug.socket(original_skt.socket)
×
2121
        return original_skt
×
2122
      end
2123

2124
      local proxy = setmetatable({
28✔
2125
        __original_socket = original_skt
14✔
2126
      }, debug_mt)
14✔
2127

2128
      return proxy
14✔
2129
    end
2130
  end
2131
end
2132

2133

2134
return copas
218✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc