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

TypeError: collection[(i + 2)] is not a function #26

Open
brian6932 opened this issue Jan 10, 2025 · 4 comments
Open

TypeError: collection[(i + 2)] is not a function #26

brian6932 opened this issue Jan 10, 2025 · 4 comments
Assignees

Comments

@brian6932
Copy link

brian6932 commented Jan 10, 2025

undefined:6
                        collection[i+2](arg0);
                                       ^

TypeError: collection[(i + 2)] is not a function
    at eval (eval at bakeCollection (.../node_modules/.pnpm/[email protected]/node_modules/tseep/lib/task-collection/bake-collection.js:65:27), <anonymous>:6:40)
    at TaskCollection.rebuild_on_first_call [as call] (.../node_modules/.pnpm/[email protected]/node_modules/tseep/lib/task-collection/task-collection.js:203:15)

Occurring on an emit:

new EventEmitter().emit(String, Object /*null prototype*/, Object)

Seemingly at random.

@Morglod
Copy link
Owner

Morglod commented Jan 11, 2025

Hi! Could you please provide more code and node version? Or just this code causes this error

@brian6932
Copy link
Author

brian6932 commented Jan 12, 2025

provide node version

v23.6.0

provide more more code? Or just this code causes this error

I don't really have an isolated report, the stacktrace doesn't really tell me much. I tried to remove most of the irrelevant code, each Connection has slightly different prototypes, but I don't really think they're that relevant to the issue. I put a try/catch on the effected line, and it seems to catch, so for now I'm dealing with it that way, I have no idea how to consistently reproduce this, it just randomly happened to me sometimes.

file1
const Connection = function (initBuffer, config, emitter) {
	this.socket = new TLSSocket
	this.socket
		.setNoDelay()
		.connect(1234, `foo.bar`)
		.on(`data`, foo => {
			let parsedCommand = new CommandParser(foo)
			emitter.emit(parsedCommand.command, parsedCommand, this) // Error on this line

			while (parsedCommand.subarray !== undefined) {
				parsedCommand = new CommandParser(parsedCommand.subarray)
				emitter.emit(parsedCommand.command, parsedCommand, this)
			}
		})
		.write(initBuffer)
}

export const
	ReadOnlyConnection = function (initBuffer, config, emitter) {
		Connection.call(this, initBuffer, config, emitter)
	},
	WriteOnlyConnection = function (initBuffer, config, emitter) {
		Connection.call(this, initBuffer, config, emitter)
	},
	ReadWriteConnection = function (initBuffer, config, emitter) {
		Connection.call(this, initBuffer, config, emitter)
	}
file2
import { EventEmitter } from 'tseep'
import { ReadOnlyConnection, WriteOnlyConnection, ReadWriteConnection } from './file1.js'
export const Client = (config = new Config) => {
	for (const i of read.keys())
		read[i] = new ReadOnlyConnection(initBuffer, config, emitter)
	for (const i of write.keys())
		write[i] = new WriteOnlyConnection(initBuffer, config, emitter)
}

@Morglod
Copy link
Owner

Morglod commented Jan 12, 2025

very strange. may be you could send whole project to my email "[email protected]" so i could debug it

looking at tseep's code it could happen only if you somehow modify private fields or it somehow adds new listener WHILE it was baking collection. but probably its something else

after listener was added, listeners should be more than 1500, and numbers of items should be mod div by %10 or %4 or %3.


also will be cool if you manually add try catch on eval inside bakeCollection, here
.../node_modules/.pnpm/[email protected]/node_modules/tseep/lib/task-collection/bake-collection.js:65

        try {
            var funcFactory = eval(funcFactoryCode);
            return funcFactory(collection);
        } catch (err) {
            console.error(err);
            console.log(collection);
            throw err;
        }

so we could look inside.

It also maybe happens because listener adds another listener of same eventName and emits same eventName, so it will be recursion.
Will add tests on this and check if it may be the problem soon.

@Morglod
Copy link
Owner

Morglod commented Jan 12, 2025

or may be recursion which clears all listeners

@Morglod Morglod self-assigned this Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants