From 8833715227cc8fc69df9e079743e9e5f1da3d00a Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 15 Apr 2026 05:21:54 +0300 Subject: [PATCH] veb: fix response being sent twice over one connection (fixes #23715) --- vlib/v/gen/c/cgen.v | 10 ++++- .../veb_html_return_in_or_block_test.html | 1 + .../tests/veb_html_return_in_or_block_test.v | 40 +++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/templates/veb_html_return_in_or_block_test.html create mode 100644 vlib/v/tests/veb_html_return_in_or_block_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index de0bbb6e5..7779eeff1 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -8488,12 +8488,18 @@ fn (mut g Gen) return_stmt(node ast.Return) { } if exprs_len > 0 { - // skip `return $vweb.html()` + // `$vweb.html()` / `$veb.html()` expand to statements, so the Result return + // needs to be emitted explicitly here. if expr0 is ast.ComptimeCall && expr0.is_vweb { g.inside_return_tmpl = true g.expr(expr0) g.inside_return_tmpl = false - g.writeln(';') + ret_typ := g.ret_styp(g.unwrap_generic(g.fn_decl.return_type)) + g.write_defer_stmts_when_needed(node.scope, true, node.pos) + if !g.is_builtin_mod { + g.autofree_scope_vars(node.pos.pos - 1, node.pos.line_nr, true) + } + g.writeln('return (${ret_typ}){0};') return } } diff --git a/vlib/v/tests/templates/veb_html_return_in_or_block_test.html b/vlib/v/tests/templates/veb_html_return_in_or_block_test.html new file mode 100644 index 000000000..169c7928b --- /dev/null +++ b/vlib/v/tests/templates/veb_html_return_in_or_block_test.html @@ -0,0 +1 @@ +Order not found! diff --git a/vlib/v/tests/veb_html_return_in_or_block_test.v b/vlib/v/tests/veb_html_return_in_or_block_test.v new file mode 100644 index 000000000..bb54ec0ca --- /dev/null +++ b/vlib/v/tests/veb_html_return_in_or_block_test.v @@ -0,0 +1,40 @@ +module main + +import veb + +struct Context { + veb.Context +} + +struct App { +mut: + continued bool +} + +struct Order {} + +fn get_order(valid bool) !Order { + if valid { + return Order{} + } + return error('missing order') +} + +fn (mut app App) index(mut ctx Context) veb.Result { + record := get_order(false) or { + ctx.res.set_status(.not_found) + return $veb.html('templates/veb_html_return_in_or_block_test.html') + } + _ = record + app.continued = true + return ctx.text('continued') +} + +fn test_veb_html_return_in_or_block_does_not_continue() { + mut app := App{} + mut ctx := Context{} + _ = app.index(mut ctx) + assert !app.continued + assert ctx.res.status_code == 404 + assert ctx.res.body.trim_space() == 'Order not found!' +} -- 2.39.5