karlseguin / http.zig

An HTTP/1.1 server for zig
MIT License
533 stars 41 forks source link

nightly version : `error: local variable is never mutated` #28

Closed xiusin closed 9 months ago

xiusin commented 9 months ago

Need to fix it?

karlseguin commented 9 months ago

Are you on the latest version of http.zig? I believe I fixed all of these shortly after Zig made the change. Also, everything seems fine on 0.12.0-dev.1861+412999621

xiusin commented 9 months ago

@karlseguin Maybe I'm not using the latest version. I'll update the dependencies and see if it works.

xiusin commented 9 months ago

@karlseguin When I updated to the latest version, the previously normal API would quit the program with an error the second time I accessed it. How should I handle this?

steps [5/9] zig build-exe xiusin-zig-examples Debug native... 
[INFO] Listening on http://localhost:8090/
[API][/api/v5/account/positions-history]use time: 871ms
[API][/api/v5/account/positions-history]use time: 2932ms
Segmentation fault at address 0x10b647000
/Users/xiusin/.cache/zig/p/1220523a844e6855b85a69e63bc3ea9f79d7b9202ebee9410ad26aa46ed62ffb5432/src/response.zig:177:42: 0x10ac9d6a5 in writeByte (xiusin-zig-examples)
            self.state.body_buffer.?.data[pos] = b;
                                         ^
???:?:?: 0x10af735ff in ??? (???)
Unwind information for `???:0x10af735ff` was not available, trace may be incomplete

???:?:?: 0x10af65097 in ??? (???)
run xiusin-zig-examples: error: the following command terminated unexpectedly:
/Users/xiusin/projects/xiusin-zig-examples/zig-out/bin/xiusin-zig-examples 
Build Summary: 7/9 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run xiusin-zig-examples failure
error: the following build command failed with exit code 1:
/Users/xiusin/projects/xiusin-zig-examples/zig-cache/o/c8d04dabfd67b67ae54b646bb7428f9b/build /opt/zig/zig /Users/xiusin/projects/xiusin-zig-examples /Users/xiusin/projects/xiusin-zig-examples/zig-cache /Users/xiusin/.cache/zig --seed 0xfa813bd3 run
karlseguin commented 9 months ago

I just pushed a commit that solves a double free related to that code. I'm not 100% sure that's your issue, but please give it a try.

If it's still happening, it might be helpful to have more information. I assume this is happening when you're using res.writer() or res.json(...) ? Are you using the default response.body_buffer_size config or a custom one?

xiusin commented 9 months ago

@karlseguin I am using the default case without adding new configurations. I will update later to see if it works. Thank you.

xiusin commented 9 months ago

@karlseguin the following errors occur in the latest version, causing the program to exit

hread 10433181 panic: index out of bounds: index 16, len 16
/Users/xiusin/.cache/zig/p/12206e54343fea853b8fe789bca0e7ac81b3109fc142100814bad90bba05798c2c92/src/buffer.zig:105:29: 0x10ed54f3d in release (xiusin-zig-examples)
                self.buffers[available] = buffer;
                            ^
/Users/xiusin/.cache/zig/p/12206e54343fea853b8fe789bca0e7ac81b3109fc142100814bad90bba05798c2c92/src/response.zig:327:37: 0x10ef839b5 in reset (xiusin-zig-examples)
            self.buffer_pool.release(buf);
                                    ^
/Users/xiusin/.cache/zig/p/12206e54343fea853b8fe789bca0e7ac81b3109fc142100814bad90bba05798c2c92/src/worker.zig:466:29: 0x10f00286d in reset (xiusin-zig-examples)
        self.res_state.reset();
                            ^
/Users/xiusin/.cache/zig/p/12206e54343fea853b8fe789bca0e7ac81b3109fc142100814bad90bba05798c2c92/src/worker.zig:726:19: 0x10ef841d6 in release (xiusin-zig-examples)
        conn.reset();
                  ^
/Users/xiusin/.cache/zig/p/12206e54343fea853b8fe789bca0e7ac81b3109fc142100814bad90bba05798c2c92/src/worker.zig:597:31: 0x10eefc902 in close (xiusin-zig-examples)
        self.conn_pool.release(conn);
                              ^
/Users/xiusin/.cache/zig/p/12206e54343fea853b8fe789bca0e7ac81b3109fc142100814bad90bba05798c2c92/src/worker.zig:125:48: 0x10ee0585c in run (xiusin-zig-examples)
                        .close => manager.close(conn),
                                               ^
/opt/zig/lib/std/Thread.zig:411:13: 0x10edb4ec3 in callFn__anon_11445 (xiusin-zig-examples)
            @call(.auto, f, args);
            ^
/opt/zig/lib/std/Thread.zig:684:30: 0x10ed55db2 in entryFn (xiusin-zig-examples)
                return callFn(f, args_ptr.*);
xiusin commented 9 months ago

An exception will be reported under the system centos


thread 24067 panic: index out of bounds: index 18446744073709551578, len 64
/home/codespace/.cache/zig/p/12206e54343fea853b8fe789bca0e7ac81b3109fc142100814bad90bba05798c2c92/src/worker.zig:973:33: 0x49b664 in wait (xiusin-zig-examples)
/home/codespace/.cache/zig/p/12206e54343fea853b8fe789bca0e7ac81b3109fc142100814bad90bba05798c2c92/src/worker.zig:96:40: 0x3b3232 in run (xiusin-zig-examples)
/opt/zig/lib/std/Thread.zig:411:13: 0x36e526 in callFn__anon_11624 (xiusin-zig-examples)
            @call(.auto, f, args);
            ^
/opt/zig/lib/std/Thread.zig:1209:30: 0x30899c in entryFn (xiusin-zig-examples)
                return callFn(f, self.fn_args);
                             ^
/opt/zig/lib/c.zig:239:13: 0x7429c0 in clone (c)
            asm volatile (
            ^
???:?:?: 0xfff in ??? (???)
Unwind information for `???:0xfff` was not available, trace may be incomplete
karlseguin commented 9 months ago

The first index out of bound (16/16 on buffer release) has been fixed by https://github.com/karlseguin/http.zig/commit/7e5f19b8d896dfc2f5396dde910f11ad106078dd I'm pretty confident in this fix..and added a test that always reproduced the issue and is now passing.

The second index out of bound (18446744073709551578/64) might be fixed by https://github.com/karlseguin/http.zig/commit/b73ef2eba22be8cfa6b3decc1b00d676cdc1834a. Clearly the epoll_wait2 is failing. I've added handling for INTR and the other possible errors are now unreachable, which is what the std lib does with epoll_wait.

xiusin commented 9 months ago

@karlseguin is ok!