Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vweb RUNTIME ERROR: invalid memory access using database (postgres) and workers #18006

Closed
Casper64 opened this issue Apr 20, 2023 · 0 comments · Fixed by #18010
Closed

vweb RUNTIME ERROR: invalid memory access using database (postgres) and workers #18006

Casper64 opened this issue Apr 20, 2023 · 0 comments · Fixed by #18010
Assignees
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@Casper64
Copy link
Member

Describe the bug

When sending simultaneous requests to a multithreaded vweb server with a database connection a runtime error occurs.

Expected Behavior

No runtime error

Current Behavior

[Vweb] Running app on http://localhost:8080/
[Vweb] We have 23 workers
message type 0x31 arrived from server while idle
message type 0x32 arrived from server while idle
message type 0x54 arrived from server while idle
message type 0x44 arrived from server while idle
message type 0x43 arrived from server while idle
message type 0x5a arrived from server while idle
7f917c1095c0 : at ???: RUNTIME ERROR: invalid memory access
7f917c1095c0 : at ???: RUNTIME ERROR: invalid memory access
7f917c1095c0 : at ???: RUNTIME ERROR: invalid memory access
7f917c1095c0 : at ???: RUNTIME ERROR: invalid memory access
7f917c1095c0 : at ???: RUNTIME ERROR: invalid memory access
7f917c1095c0 : at ???: RUNTIME ERROR: invalid memory access

Reproduction Steps

Create vweb app:

module main

import vweb
import db.pg

struct App {
	vweb.Context
pub mut:
	db pg.DB
}

[table: 'users']
struct User {
	id int [primary; sql: serial]
	name string [nonull]
}

fn main() {
	mut db := pg.connect(user: 'dev', password: 'password', dbname: 'db_name')!

	sql db {
		create table User
	}!

	sql db {
		delete from User where name == 'casper64'
	}!

	test_user := User{name: 'casper64'}
	sql db {
		insert test_user into User
	}!


	vweb.run_at(&App{
		db: db,
	}, port: 8080)!
}

pub fn (mut app App) index() vweb.Result {
	rows := sql app.db {
		select from User where name == 'casper64'
	} or { []User{} }

	if rows.len == 0 {
		return app.not_found()
	}

	user := rows[0]
	return app.text(user.name)
}

Send multiple requests with wrk

wrk -t 4 -c 20 -d10s http://localhost:8080/

Possible Solution

Currently 1 database connection is shared between all threads. To fix this issue a database pool can be implemented where each worker has it's own connection.

Acknowledgments

I might be able to fix this.

Additional Information/Context

The bug doesn't occur when using a file db like an sqlite file.

mut db := sqlite.connect('db.sqlite3') or { panic(err) }

V version

V 0.3.3 4e498b4.adcd16b

Environment details (OS name and version, etc.)

V full version: V 0.3.3 4e498b4.adcd16b
OS: linux, Kali GNU/Linux Rolling (WSL 2)
Processor: 24 cpus, 64bit, little endian, 12th Gen Intel(R) Core(TM) i9-12900K

getwd: /home/casper
vexe: /opt/v/v
vexe mtime: 2023-04-20 15:59:20

vroot: OK, value: /opt/v
VMODULES: OK, value: /home/casper/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.39.2
Git vroot status: weekly.2023.15-38-gadcd16b1
.git/config present: true

CC version: cc (Debian 12.2.0-14) 12.2.0
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

@Casper64 Casper64 added the Bug This tag is applied to issues which reports bugs. label Apr 20, 2023
@Casper64 Casper64 self-assigned this Apr 20, 2023
@Casper64 Casper64 linked a pull request Apr 22, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant