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

Duplicates?... #117

Closed
brandonros opened this issue Jul 31, 2015 · 23 comments
Closed

Duplicates?... #117

brandonros opened this issue Jul 31, 2015 · 23 comments
Labels
more info needed Issue not actionable w/out additional details

Comments

@brandonros
Copy link

node-uuid is required in paypal-rest-sdk and called on line 60 in this file. For some reason, I'm getting returned the same uuid consecutively. I don't know if it is paypal-rest-sdk, node-uuid, Mac OS X, pm2? I've tried restarting all of my instances. I'm about to console.log the returned UUID.

I get 'e7d48462-0cdb-4291-a4c7-8adeade30193' repeatedly.

@broofa
Copy link
Member

broofa commented Jul 31, 2015

'Looks like a valid v4 id.

If possible, I would suggest stepping into the uuid.v4() method to see if it's actually generating the same random #'s each time at this line. Also, do _rng.toString() while stopped in that function to see which logic it's using for random # generation.

@Misterhex
Copy link

+1, i'm getting duplicates as well, am i missing something?

@tracker1
Copy link

Could it be the compiler is doing a weird optimization having _rnds defined outside the function scope? It may be better to include a more better random number generator as a fallback [1]. It would help to know which random number generator is being used, or at least the full browser/os versions.

[1] https://www.npmjs.com/package/random-js

@coolaj86
Copy link
Contributor

Can you guys test this now? It sounds like it may have been related to the recently closed bug where Math.random(), which isn't very random, was being used accidentally.

@nebulou5
Copy link

nebulou5 commented Jul 8, 2016

Also getting duplicates when using uuid.v1() as a default mongoose field.

@antoine-duchenet
Copy link

antoine-duchenet commented Jul 11, 2016

Also getting duplicates with uuid.v4(), a server restart is required to change the value.
However, I use this library in two projects, and the second one has no duplicates problem, even if the uuid.v4() is used the exact same way.

I fixed it by wrapping the library in a service to force the runtime evaluation of the v4() method :

const uuid = require('node-uuid');

module.exports = {
    v4: () => uuid.v4,
};

@raditha
Copy link

raditha commented Jan 29, 2017

+1 Is there a fix for the duplicate ids?

@broofa
Copy link
Member

broofa commented Jan 30, 2017

Haven't seen a recipe to reproduce this. Not enough info to diagnose. Until that changes this isn't actionable.

@jfdoube
Copy link

jfdoube commented May 5, 2018

I have been experiencing the same issue with an aws lambda and nodejs8.10 container
Use a docker container or aws lambda, might be easier to reproduce

@ddellamico
Copy link

We have the exact same issue only running on docker containers... some suggestions ?

@broofa
Copy link
Member

broofa commented May 23, 2018

I'll reopen if/when someone provides a SCCEE for reproducing the problem. Better still, identify a fix and submit a PR.

(My apologies but I simply don't have time to chase this down based on the vague descriptions given so far.)

@nebulou5
Copy link

Refer to antoine-duchenet's post above. If you call the function directly as a property value (for example) without forcing it to evaluate fresh at run-time, you'll just end up with a static value. Hard to say that's the issue everyone is experiencing, but in my case it was the solution.

export { myProperty: uuid.v4() } vs export { myProperty: () => uuid.v4() }

@broofa
Copy link
Member

broofa commented May 23, 2018

@FuzzySockets: I'm confused. What you're describing is exactly what I would expect. The first case is just assigning the result of uuid.v4() to myProperty. Why would you expect anything other than a static uuid value there?

> const before = { myProperty: uuid.v4() }
undefined
> before.myProperty
'bdf9794f-0897-48dd-8fed-da380dac6e74'

> const after = { myProperty: () => uuid.v4() }
undefined
> after.myProperty
[Function: myProperty]
> after.myProperty()
'40848bdb-7c04-40e2-8694-5513db705732'
> after.myProperty()
'd1edd511-6ce9-4a12-b196-4fafb6b3b16f'
> after.myProperty()
'1258559b-c082-436c-8ecb-f74679954ffd'
> // Etc ...

@nebulou5
Copy link

@broofa At the time I was following along with an example that (incorrectly) showed how to generate unique uuids for a mongoose model. Without having ever used node-uuid before, you might just assume either uuid returns a random generator function vs a static value which is where my error resided, or that mongoose encloses the model object, therefore calling the function each time you instantiate the model.

@alkhachatryan
Copy link

2022 Jun. Getting duplicates

@broofa broofa added the more info needed Issue not actionable w/out additional details label Jun 10, 2022
@broofa
Copy link
Member

broofa commented Jun 10, 2022

@Rasfuranku
Copy link

Rasfuranku commented Nov 30, 2022

I have an action that will get two unique tasks from an array of ten items. The action is triggered manually. If the same task is selected on different actions the id assigned is repeated.

export const getTasks = (): ITask[] => {
    const selectedTasks = [];
    const tasksCopy: ITask[] = [...tasks];
    
    for (let i = 0; i < 2; i++) {
        const index = randomIndex(tasksCopy.length);
        const task = tasksCopy.splice(index, 1);
        task[0].id = uuidv4();
        task[0].status = TaskStatus.processing;
        selectedTasks.push(task[0]);
    }

    return selectedTasks;
}

@broofa
Copy link
Member

broofa commented Nov 30, 2022

@Rasfuranku: On the rare occasion we've seen issues with duplicates, it's most often because the runtime involved provides a random-number API (e.g. crypto.getRandomValues()) that deliberately returns non-random values.

Provide a Minimal, Complete, and Verifiable example] and I'll be happy to take a look.

@Rasfuranku
Copy link

@broofa Thanks for the reply and your help. Here is the code flow of how it is used; I hope it is enough:

When creating a second or third bot with a task already assigned to a previous bot, the id for the task gets repeated.

Node version: v16.17.1
npm version: 8.15.0
react: 18.2.0
uuid: 9.0.0

React component: Here, I am also using uuidjs to generate ids for the Bot objects, which works just fine.

import { useState, FC } from 'react'
import { Button, TextField } from "@mui/material";
import { v4 as uuidv4 } from 'uuid';

import { getTasks } from '../utils/utils';
import ITask from '../../models/ITask';
import IBot from '../../models/IBot';

const Controls: FC<{ handleAddNewBot: any, socket: any }> = ({ handleAddNewBot, socket }) => {    
    const createBot = (name: string) => {
        const tasks: ITask[] = getTasks();
        const newBot: IBot = {
            id: uuidv4(), // This works fine
            name,
            tasks,
        }
        socket.emit(newBot);
    }

    return (
        <div>
            <TextField
                autoFocus
                margin="dense"
                id="name"
                label="Bot name"
                type="text"
                fullWidth
                variant="outlined"
                value={botName}
                onChange={setName}
            />
            <Button
                variant="contained"
                onClick={() => createBot(botName)}
                color="primary">
                New Bot
            </Button>
        </div>
    );
}

export default Controls;

The function where UUIDs generate repeated ids.

import { v4 as uuidv4 } from 'uuid';

export const getTasks = (): ITask[] => {
    const selectedTasks = [];
    const tasksCopy: ITask[] = [...tasks];
    
    for (let i = 0; i < 2; i++) {
        const index = randomIndex(tasksCopy.length);
        const task = tasksCopy.splice(index, 1);
        task[0].id = uuidv4(); // Duplicated ids
        task[0].status = TaskStatus.processing;
        selectedTasks.push(task[0]);
    }

    return selectedTasks;
}

The tasks inside the getTask function come from a static file with ten items, with an empty id by default; they do not change and get randomly assigned in pairs when a new bot is created.

task Array example:

const tasks: ITask[] = [
    {
        description: 'Task 1',
    },
    {
        id: "",
        description: 'Task 2', 
    },
    {
        id: "",
        description: 'Task 3', 
    },
    {
        id: "",
        description: 'Task 4', 
    },
    {
        id: "",
        description: 'Task 5', 
    },
    {
        id: "",
        description: 'Task 6', 
    },
    {
        id: "",
        description: 'Task 7', 
    },
    {
        id: "",
        description: 'Task 8', 
    },
    {
        id: "",
        description: 'Task 9', 
    },
    {
        id: "",
        description: 'Task 10', 
    }
];

@broofa
Copy link
Member

broofa commented Nov 30, 2022

I hope it is enough

@Rasfuranku Sadly, it's not. Please carefully read the article I linked to, especially the parts about an example being reproducible and verifiable.

@Rasfuranku
Copy link

@broofa I found that I was an idiot, and I was mutating the object before adding it to the array. Thanks for your time.

@t1maccapp
Copy link

This could happen in Lambda env. More context here: https://stackoverflow.com/questions/65369650/aws-lambda-and-uuid-it-always-return-same-value

@uuidjs uuidjs locked as off-topic and limited conversation to collaborators Feb 2, 2023
@broofa
Copy link
Member

broofa commented Feb 2, 2023

@t1maccapp: SO question is a result of user error and is not relevant to this project (as is clearly explained by the one and only answer there.)


Locking thread as it's been a while since there was any productive conversation.

If you're contemplating reporting an issue with uuid duplicates...

You're welcome to do so, but please provide a minimum reproduction, and double check your work. It's far more likely the problem is with how this project is used rather than with the project itself.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
more info needed Issue not actionable w/out additional details
Projects
None yet
Development

No branches or pull requests