File: debugging.md

package info (click to toggle)
ruby-async 2.36.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 400 kB
  • sloc: ruby: 1,938; makefile: 4
file content (63 lines) | stat: -rw-r--r-- 2,055 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Debugging

This guide explains how to debug issues with programs that use Async.

## Debugging Techniques

### Debugging with `puts`

The simplest way to debug an Async program is to use `puts` to print messages to the console. This is useful for understanding the flow of your program and the values of variables. However, it can be difficult to use `puts` to debug programs that use asynchronous code, as the output may be interleaved. To prevent this, wrap it in `Fiber.blocking{}`:

```ruby
require "async"

Async do
	3.times do |i|
		sleep i
		Fiber.blocking{puts "Slept for #{i} seconds."}
	end
end
```

Using `Fiber.blocking{}` prevents any context switching until the block is complete, ensuring that the output is not interleaved and that flow control is strictly sequential. You should not use `Fiber.blocking{}` in production code, as it will block the reactor.

### Debugging with IRB

You can use IRB to debug your Async program. In some cases, you will want to stop the world and inspect the state of your program. You can do this by wrapping `binding.irb` inside a `Fiber.blocking{}` block:

```ruby
Async do
	3.times do |i|
		sleep i
		# The event loop will stop at this point and you can inspect the state of your program.
		Fiber.blocking{binding.irb}
	end
end
```

If you don't use `Fiber.blocking{}`, the event loop will continue to run and you will end up with three instances of `binding.irb` running.

### Debugging with `Async::Debug`

The `async-debug` gem provides a visual debugger for Async programs. It is a powerful tool that allows you to inspect the state of your program and see the hierarchy of your program:

```ruby
require "async"
require "async/debug"

Sync do
	debugger = Async::Debug.serve
	
	3.times do
		Async do |task|
			while true
				duration = rand
				task.annotate("Sleeping for #{duration} second...")
				sleep(duration)
			end
		end
	end
end
```

When you run this program, it will start a web server on `http://localhost:9000`. You can open this URL in your browser to see the state of your program.